Python计算程序运行时间方法
说到某段程序时间的运行时长,其实大家的思路都一样,就是在该程序的开始获取以下当前系统的时间,在运行完后再获取一次当前系统的时间,两次时间相减就是我们所要获得的该段程序的运行时长。但是对于Python而言,要获取这个时间的方式比较多,并且方式之间存在一定的差异。下面我们先介绍相关的方法,然后再谈谈之间的不同。文章后面部分我们会使用Python提供的另一种库(timeit)来计算这个时间。
方法1、time.time()获取
import time
start = time.time()
def sum_func(num):
    sum = 0
    for i in range(1, num + 1):
        sum += i
    return sum
result = sum_func(100)
print(result)
end = time.time()
print('Running time: %s Seconds' % (end - start))
运行结果(Windows)[运行多次]:
(venv) E:\python\test>python test.py 5050 Running time: 0.0004951953887939453 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0.0 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0.0 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0.00049591064453125 Seconds
运行结果(Centos)[运行多次]:
vagrant@homestead:~$ python test.py 5050 Running time: 0.000318050384521 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 0.00025200843811 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 0.000232934951782 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 0.000305891036987 Seconds
从上面的运行结果可以看出,使用time.time()在windows上的精度不是特别高,但是在Linux系统上的精度很高。
方法2、time.clock()
import time start = time.clock() def sum_func(num): sum = 0 for i in range(1, num + 1): sum += i return sum result = sum_func(100) print(result) end = time.clock() print('Running time: %s Seconds' % (end - start))
运行结果(Windows)[运行多次]:
(venv) E:\python\test>python test.py 5050 Running time: 9.838011325349381e-05 Seconds (venv) E:\python\test>python test.py 5050 Running time: 6.699720974252268e-05 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0.0001019062821873108 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0.00010261151597407419 Seconds
运行结果(Centos)[运行多次]:
vagrant@homestead:~$ python test.py 5050 Running time: 5e-05 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 8.3e-05 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 3.4e-05 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 4.1e-05 Seconds
从上面的运行结果可以看出,相对来说windows返回的精度比Linux的精度相对要高些。所以Python的标准库手册推荐在任何情况下尽量使用time.clock()。
方法3、datetime模块
import datetime
start = datetime.datetime.now()
def sum_func(num):
    sum = 0
    for i in range(1, num + 1):
        sum += i
    return sum
result = sum_func(100)
print(result)
end = datetime.datetime.now()
print('Running time: %s Seconds' % (end - start))
运行结果(Windows)[运行多次]:
(venv) E:\python\test>python test.py 5050 Running time: 0:00:00.000495 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0:00:00 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0:00:00.000496 Seconds (venv) E:\python\test>python test.py 5050 Running time: 0:00:00 Seconds
运行结果(Centos)[运行多次]:
vagrant@homestead:~$ python test.py 5050 Running time: 0:00:00.000258 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 0:00:00.000377 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 0:00:00.000286 Seconds vagrant@homestead:~$ python test.py 5050 Running time: 0:00:00.000338 Seconds
从上面的运行结果可以看出,使用datetime.datetime.now()在windows和linux的精度都不太高,所以这个方式获取的效率并不好,不提倡使用该方式。
在这里其实还需要注意下,time.clock()只计算了当前程序使用CPU的时间,而其他两种方式还包含了其他程序使用CPU的时间。那么对于time模块的time.time()和time.clock()到底哪个精度高呢?这个可以去查阅time模块的文档。总体来讲,这个分平台而定,Windows平台建议使用time.clock(),Linux系统使用time.time()。
Python的计时库timeit的使用
timeit库是一个Python的计时库,这个能确保跨平台的精度性。使用方式如下两种
#第一种
import timeit
start = timeit.default_timer()
def sum_func(num):
    sum = 0
    for i in range(1, num + 1):
        sum += i
    return sum
sum_func(100)
end = timeit.default_timer()
print('Running time: %s Seconds' % (end - start))
上面的思想也是跟前面一样,分别获取两次的时间然后相减。
#第二种
import timeit
def sum_func(num):
    sum = 0
    for i in range(1, num + 1):
        sum += i
    return sum
print(timeit.timeit('sum_func(100)', 'from __main__ import sum_func', number=1))
直接使用测试函数的执行时间方式,这里只执行了一次,默认执行次数为1000000次,更多相关使用方式请自行百度。
总结:如果后期如果需要计算程序的运行时间,最好使用timeit库来进行获取,其次再根据平台来选择其他方式来获取运行时间。
 在使用Docker时,经常会犯一个错,误以为latest镜像会自己更像到最小版本,其实这样理解是有问题的,latest就是一个标签,没有自动更新到最新版本的功能,本文就是对latest标签进行介绍。
在使用Docker时,经常会犯一个错,误以为latest镜像会自己更像到最小版本,其实这样理解是有问题的,latest就是一个标签,没有自动更新到最新版本的功能,本文就是对latest标签进行介绍。 nodejs中使用npm和yarn,使用最新阿里云镜像 aliyun mirror,网上很多还是文章用的是下面这个地址~~yarn config set registry https://registry.npm.taobao.org~~
nodejs中使用npm和yarn,使用最新阿里云镜像 aliyun mirror,网上很多还是文章用的是下面这个地址~~yarn config set registry https://registry.npm.taobao.org~~ 《是妈妈是女儿》聚焦母女间未曾言明的爱意,以书信对话的形式呈现出各自的内心独白,表达彼此的牵挂。黄绮珊与希林娜依·高用跨越时空、打开心扉、深情对唱的形式,将天下母女爱的寄语化作心灵的倾诉。黄绮珊的每一句话,每一个字都演绎出了妈妈对女儿的爱,而希林依娜·高把女儿对妈妈的爱由不理解到理解再到感恩演绎得淋漓尽致。
《是妈妈是女儿》聚焦母女间未曾言明的爱意,以书信对话的形式呈现出各自的内心独白,表达彼此的牵挂。黄绮珊与希林娜依·高用跨越时空、打开心扉、深情对唱的形式,将天下母女爱的寄语化作心灵的倾诉。黄绮珊的每一句话,每一个字都演绎出了妈妈对女儿的爱,而希林依娜·高把女儿对妈妈的爱由不理解到理解再到感恩演绎得淋漓尽致。 Towxml 是一个让小程序(微信/QQ)可以解析Markdown、HTML的解析库。能够使小程序完美解析Markdown内容。
Towxml 是一个让小程序(微信/QQ)可以解析Markdown、HTML的解析库。能够使小程序完美解析Markdown内容。 Editor.md 是一个可嵌入的开源 Markdown 在线编辑器组件,你可以很方便用在浏览器、NW.js(Node-webkit)等地方,基于CodeMirror、jQuery 和 Marked 构建。
Editor.md 是一个可嵌入的开源 Markdown 在线编辑器组件,你可以很方便用在浏览器、NW.js(Node-webkit)等地方,基于CodeMirror、jQuery 和 Marked 构建。 快速生成表格
快速生成表格 Electron页面跳转、浏览器打开链接和打开新窗口
Electron页面跳转、浏览器打开链接和打开新窗口 Docker编译镜像出现:fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.12/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.2c4ac24e.tar.gz: No such file or directory问题
Docker编译镜像出现:fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.12/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.2c4ac24e.tar.gz: No such file or directory问题 在Mac电脑中,如何对Git的用户名和密码进行修改呢?起初不懂Mac,所以整了很久,本文将记录如何对这个进行操作,以便后期使用。
在Mac电脑中,如何对Git的用户名和密码进行修改呢?起初不懂Mac,所以整了很久,本文将记录如何对这个进行操作,以便后期使用。 在使用Git的过程中,不想每次都输入用户名和密码去拉取代码,所以就需要保存这些信息,那么既然有保存了,就必须有清除功能。
在使用Git的过程中,不想每次都输入用户名和密码去拉取代码,所以就需要保存这些信息,那么既然有保存了,就必须有清除功能。