我有一个用Python编写的命令行程序,运行时间比较长。我想知道它完成运行所需的精确时间。
我查看了timeit
模块,但是它似乎只适用于小片段的代码。我想计时整个程序的运行时间。
我有一个用Python编写的命令行程序,运行时间比较长。我想知道它完成运行所需的精确时间。
我查看了timeit
模块,但是它似乎只适用于小片段的代码。我想计时整个程序的运行时间。
在单元格中,您可以使用Jupyter的%%time
魔法命令来测量执行时间:
%%time
[ x**2 for x in range(10000)]
CPU times: user 4.54 ms, sys: 0 ns, total: 4.54 ms
Wall time: 4.12 ms
example_notebook.ipynb
。在同一目录下的新笔记本中:
# Convert your notebook to a .py script:
!jupyter nbconvert --to script example_notebook.ipynb
# Run the example_notebook with -t flag for time
%run -t example_notebook
IPython CPU timings (estimated):
User : 0.00 s.
System : 0.00 s.
Wall time: 0.00 s.
<HH:MM:SS>
格式打印经过的时间。import time
from datetime import timedelta
start_time = time.time()
#
# Perform lots of computations.
#
elapsed_time_secs = time.time() - start_time
msg = "Execution took: %s secs (Wall clock time)" % timedelta(seconds=round(elapsed_time_secs))
print(msg)
与@rogeriopvl的回答类似,我稍作修改,使用同一库将时间转换为小时、分钟和秒钟,用于长时间运行的作业。
import time
start_time = time.time()
main()
seconds = time.time() - start_time
print('Time Taken:', time.strftime("%H:%M:%S",time.gmtime(seconds)))
样例输出
Time Taken: 00:00:08
对于函数,我建议使用我创建的这个简单的装饰器。
def timeit(method):
def timed(*args, **kwargs):
ts = time.time()
result = method(*args, **kwargs)
te = time.time()
if 'log_time' in kwargs:
name = kwargs.get('log_name', method.__name__.upper())
kwargs['log_time'][name] = int((te - ts) * 1000)
else:
print('%r %2.22f ms' % (method.__name__, (te - ts) * 1000))
return result
return timed
@timeit
def foo():
do_some_work()
# foo()
# 'foo' 0.000953 ms
kwargs
中,那么kwargs.get('log_name',...)
等同于kwargs['log_name']
,不是吗?from time import time
start_time = time()
...
end_time = time()
time_taken = end_time - start_time # time_taken is in seconds
hours, rest = divmod(time_taken,3600)
minutes, seconds = divmod(rest, 60)
我已经查看了timeit模块,但它似乎只适用于小代码段。我想计时整个程序。
$ python -mtimeit -n1 -r1 -t -s "from your_module import main" "main()"
它运行your_module.main()
函数一次,并使用time.time()
函数作为计时器打印经过的时间。
要在Python中模拟/usr/bin/time
功能,请参见Python subprocess with /usr/bin/time: how to capture timing info but ignore all other output?。
要测量每个函数的CPU时间(例如,不包括time.sleep()
期间的时间),可以使用profile
模块(Python 2上的cProfile
):
$ python3 -mprofile your_module.py
如果你想使用与profile
模块相同的计时器,可以在上面的timeit
命令中传入-p
参数。
我在许多地方都遇到了同样的问题,因此我创建了一个方便的包horology
。您可以使用pip install horology
安装它,然后以优雅的方式执行:
from horology import Timing
with Timing(name='Important calculations: '):
prepare()
do_your_stuff()
finish_sth()
将输出:
Important calculations: 12.43 ms
或者更简单的方法(如果您只有一个函数):
from horology import timed
@timed
def main():
...
将输出:
main: 7.12 h
它处理单位和舍入。它适用于Python 3.6或更新版本。
main.interval
。 - hans我也喜欢Paul McGuire的回答,但我想出了一种适合我需求更多的上下文管理器形式。
import datetime as dt
import timeit
class TimingManager(object):
"""Context Manager used with the statement 'with' to time some execution.
Example:
with TimingManager() as t:
# Code to time
"""
clock = timeit.default_timer
def __enter__(self):
"""
"""
self.start = self.clock()
self.log('\n=> Start Timing: {}')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""
"""
self.endlog()
return False
def log(self, s, elapsed=None):
"""Log current time and elapsed time if present.
:param s: Text to display, use '{}' to format the text with
the current time.
:param elapsed: Elapsed time to display. Dafault: None, no display.
"""
print s.format(self._secondsToStr(self.clock()))
if(elapsed is not None):
print 'Elapsed time: {}\n'.format(elapsed)
def endlog(self):
"""Log time for the end of execution with elapsed time.
"""
self.log('=> End Timing: {}', self.now())
def now(self):
"""Return current elapsed time as hh:mm:ss string.
:return: String.
"""
return str(dt.timedelta(seconds = self.clock() - self.start))
def _secondsToStr(self, sec):
"""Convert timestamp to h:mm:ss string.
:param sec: Timestamp.
"""
return str(dt.datetime.fromtimestamp(sec))
from time import monotonic
start_time = monotonic()
# something
print(f"Run time {monotonic() - start_time} seconds")
或者使用装饰器:
from time import monotonic
def record_time(function):
def wrap(*args, **kwargs):
start_time = monotonic()
function_return = function(*args, **kwargs)
print(f"Run time {monotonic() - start_time} seconds")
return function_return
return wrap
@record_time
def your_function():
# something