Jam*_*mes 134 python datetime execution-time
我在python脚本中有以下代码:
def fun():
#Code here
fun()
Run Code Online (Sandbox Code Playgroud)
我想执行这个脚本,并找出在几分钟内执行所需的时间.我如何知道执行此脚本需要多长时间?一个例子将非常感激.
Pet*_*nov 240
from datetime import datetime
startTime = datetime.now()
#do something
#Python 2:
print datetime.now() - startTime
#Python 3:
print(datetime.now() - startTime)
Run Code Online (Sandbox Code Playgroud)
Roy*_*Roy 121
您是在Linux或UNIX上从命令行执行脚本吗?在这种情况下,你可以使用
time ./script.py
Run Code Online (Sandbox Code Playgroud)
Dou*_* AA 55
import time
start = time.time()
fun()
# python 2
print 'It took', time.time()-start, 'seconds.'
# python 3
print('It took', time.time()-start, 'seconds.')
Run Code Online (Sandbox Code Playgroud)
Dar*_*Yin 14
我通常做的是使用clock()或time()来自time图书馆.clock测量翻译时间,同时time测量系统时间.可以在文档中找到其他警告.
例如,
def fn():
st = time()
dostuff()
print 'fn took %.2f seconds' % (time() - st)
Run Code Online (Sandbox Code Playgroud)
或者,您也可以使用timeit.我经常使用这种time方法,因为我可以快速地解决它,但是如果你计算一个可以隔离的代码片段,timeit就会派上用场.
def test():
"Stupid test function"
L = []
for i in range(100):
L.append(i)
if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()
Run Code Online (Sandbox Code Playgroud)
然后转换为分钟,您可以简单地除以60.如果您希望脚本运行时以易于阅读的格式,无论是秒还是几天,您都可以转换为a timedelta和str它:
runtime = time() - st
print 'runtime:', timedelta(seconds=runtime)
Run Code Online (Sandbox Code Playgroud)
那将打印出一些形式的东西[D day[s], ][H]H:MM:SS[.UUUUUU].您可以查看timedelta文档.
最后,如果你真正想要的是分析你的代码,那么Python也可以提供配置文件库.
小智 14
import time
startTime = time.time()
# Your code here !
print ('The script took {0} second !'.format(time.time() - startTime))
Run Code Online (Sandbox Code Playgroud)
以前的代码对我没用问题!
Pav*_*dis 12
import sys
import timeit
start = timeit.default_timer()
#do some nice things...
stop = timeit.default_timer()
total_time = stop - start
# output running time in a nice format.
mins, secs = divmod(total_time, 60)
hours, mins = divmod(mins, 60)
sys.stdout.write("Total running time: %d:%d:%d.\n" % (hours, mins, secs))
Run Code Online (Sandbox Code Playgroud)
Rap*_*pid 10
使用timeit模块.这很容易.运行你的example.py文件使它在Python Shell中处于活动状态,你现在应该可以在shell中调用你的函数了.尝试一下检查它是否有效
>>>fun(input)
output
Run Code Online (Sandbox Code Playgroud)
好,有效,现在导入timeit并设置一个计时器
>>>import timeit
>>>t = timeit.Timer('example.fun(input)','import example')
>>>
Run Code Online (Sandbox Code Playgroud)
现在我们设置了计时器,我们可以看到它需要多长时间
>>>t.timeit(number=1)
some number here
Run Code Online (Sandbox Code Playgroud)
我们走了,它会告诉你执行该功能需要多少秒(或更少).如果它是一个简单的函数,那么你可以将它增加到t.timeit(数字= 1000)(或任何数字!),然后将答案除以数字以得到平均值.
我希望这有帮助.
zab*_*bop 10
更好的是time.perf_counter():
t0 = time.perf_counter()\nfun()\nt1 = time.perf_counter()\nprint(t1-t0)\n\n# and if you really want your answer in minutes:\nprint(f"In minutes: {(t1-t0)/60}")\nRun Code Online (Sandbox Code Playgroud)\n\n文件:
\n\n\n\n
time.perf_counter()\xe2\x86\x92 float返回性能计数器的值(以秒为单位),\ni.e 具有最高可用分辨率的时钟,可测量较短的持续时间。它确实包括睡眠期间经过的时间,并且是\n系统范围内的。返回值的参考点未定义,因此只有两次调用结果之间的差异才有效。
\n用于
\nperf_counter_ns()避免float类型造成的精度损失。3.3 版本中的新功能。
\n版本 3.10 中的更改:在 Windows 上,该功能现在是系统范围的。
\n
%timeit&%time如果您使用 Jupyter Notebook(例如Google Colab),则可以使用 IPython Magic Commands。
\n例子:
\nimport time\nimport numpy as np\nnp.random.seed(42)\n\ndef fun(): \n time.sleep(0.1+np.random.rand()*0.05)\nRun Code Online (Sandbox Code Playgroud)\n然后在一个单独的单元格中,对函数进行多次计时:
\n%timeit fun()\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n10 loops, best of 5: 120 ms per loop\nRun Code Online (Sandbox Code Playgroud)\n仅对函数计时一次:
\n%time fun()\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nCPU times: user 0 ns, sys: 621 \xc2\xb5s, total: 621 \xc2\xb5s\nWall time: 114 ms\nRun Code Online (Sandbox Code Playgroud)\n您可以在此处找到有关 Magic Commands 的更多信息。
\n| 归档时间: |
|
| 查看次数: |
159507 次 |
| 最近记录: |