Answers:
Python中最简单的方法:
import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))
假设您的程序至少需要十分之一秒才能运行。
印刷品:
--- 0.764891862869 seconds ---
round(time.time() - start_time, 2)
(或您想要的任何小数),我会得到像1.24e-5这样的科学数字。
'%.2f'
代替round()
这里。
我将此timing.py
模块放入自己的site-packages
目录中,然后将其插入import timing
模块顶部:
import atexit
from time import clock
def secondsToStr(t):
return "%d:%02d:%02d.%03d" % \
reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
[(t*1000,),1000,60,60])
line = "="*40
def log(s, elapsed=None):
print line
print secondsToStr(clock()), '-', s
if elapsed:
print "Elapsed time:", elapsed
print line
print
def endlog():
end = clock()
elapsed = end-start
log("End Program", secondsToStr(elapsed))
def now():
return secondsToStr(clock())
start = clock()
atexit.register(endlog)
log("Start Program")
timing.log
如果要显示的程序中有重要的阶段,我也可以从程序中调用。但仅包括即可import timing
打印开始时间和结束时间以及总体经过时间。(请原谅我晦涩的secondsToStr
功能,它只是将秒的浮点数格式设置为hh:mm:ss.sss形式。)
from functools import reduce
,在顶部添加 并在每个print语句周围放置方括号。很棒!
在Linux或Unix中:
$ time python yourprogram.py
在Windows中,请参见以下StackOverflow问题: 如何在Windows命令行上测量命令的执行时间?
要获得更详细的输出,
$ time -v python yourprogram.py
Command being timed: "python3 yourprogram.py"
User time (seconds): 0.08
System time (seconds): 0.02
Percent of CPU this job got: 98%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.10
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 9480
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 1114
Voluntary context switches: 0
Involuntary context switches: 22
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
secondsToStr()
功能。
我真的很喜欢Paul McGuire的答案,但是我使用Python3。因此,对于那些感兴趣的人:这是他的答案的一种修改,可用于* nix上的Python 3(我想在Windows下,clock()
应该使用代替time()
):
#python3
import atexit
from time import time, strftime, localtime
from datetime import timedelta
def secondsToStr(elapsed=None):
if elapsed is None:
return strftime("%Y-%m-%d %H:%M:%S", localtime())
else:
return str(timedelta(seconds=elapsed))
def log(s, elapsed=None):
line = "="*40
print(line)
print(secondsToStr(), '-', s)
if elapsed:
print("Elapsed time:", elapsed)
print(line)
print()
def endlog():
end = time()
elapsed = end-start
log("End Program", secondsToStr(elapsed))
start = time()
atexit.register(endlog)
log("Start Program")
如果您认为此方法有用,则仍应投票赞成他的答案,而不是像他所做的大部分工作一样;)。
timedelta(seconds=t).total_seconds()
有帮助。
import time
start_time = time.clock()
main()
print time.clock() - start_time, "seconds"
time.clock()
返回处理器时间,这使我们只能计算该进程使用的时间(无论如何在Unix上)。该文档说“无论如何,这是用于基准化Python或计时算法的功能”
我喜欢输出 datetime
模块提供,其中时间增量对象根据需要以人类可读的方式显示天,小时,分钟等。
例如:
from datetime import datetime
start_time = datetime.now()
# do your work here
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))
样品输出,例如
Duration: 0:00:08.309267
要么
Duration: 1 day, 1:51:24.269711
正如JF Sebastian提到的那样,这种方法在本地时间可能会遇到一些棘手的情况,因此使用起来更安全:
import time
from datetime import timedelta
start_time = time.monotonic()
end_time = time.monotonic()
print(timedelta(seconds=end_time - start_time))
timedelta(seconds=time.monotonic()-start)
在这里使用(或者time.time()
如果间隔很大)。不要减去代表本地时间的朴素的datetime对象;本地时间不单调
start_time = time.monotonic(); end_time = time.monotonic(); timedelta(seconds=end_time - start_time)
。我相信您是对的,但是回来时还必须对其进行格式化datetime.timedelta(0, 0, 76)
。此外,似乎在Python 3只添加了单调方法
str()
为“人类”。我将更新答案,谢谢。
您可以使用Python探查器cProfile来测量CPU时间,还可以测量每个函数内部花费了多少时间以及每个函数被调用了多少次。如果您想在不知道从哪里开始的情况下提高脚本性能,这将非常有用。另一个Stack Overflow问题的答案非常好。看看文档总是很高兴也。
这是一个示例,如何从命令行使用cProfile来分析脚本:
$ python -m cProfile euler048.py
1007 function calls in 0.061 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.061 0.061 <string>:1(<module>)
1000 0.051 0.000 0.051 0.000 euler048.py:2(<lambda>)
1 0.005 0.005 0.061 0.061 euler048.py:2(<module>)
1 0.000 0.000 0.061 0.061 {execfile}
1 0.002 0.002 0.053 0.053 {map}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler objects}
1 0.000 0.000 0.000 0.000 {range}
1 0.003 0.003 0.003 0.003 {sum}
X function calls in Y CPU seconds
。如果要使用挂钟时间,请在此处使用其他答案之一。
对于Linux甚至更好: time
$ time -v python rhtest2.py
Command being timed: "python rhtest2.py"
User time (seconds): 4.13
System time (seconds): 0.07
Percent of CPU this job got: 91%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.58
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 0
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 15
Minor (reclaiming a frame) page faults: 5095
Voluntary context switches: 27
Involuntary context switches: 279
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
time.clock()
从版本3.3开始不推荐使用:此功能的行为取决于平台:根据您的要求,使用perf_counter()或process_time()来具有明确定义的行为。
time.perf_counter()
返回性能计数器的值(以小数秒为单位),即具有最高可用分辨率的时钟以测量较短的持续时间。它的确包含整个系统的睡眠时间。
time.process_time()
返回当前进程的系统和用户CPU时间之和的值(以秒为单位)。它不包括睡眠期间经过的时间。
start = time.process_time()
... do something
elapsed = (time.process_time() - start)
只需使用该timeit
模块。它同时适用于Python 2和Python 3。
import timeit
start = timeit.default_timer()
# All the program statements
stop = timeit.default_timer()
execution_time = stop - start
print("Program Executed in "+str(execution_time)) # It returns time in seconds
它以秒为单位返回,您可以拥有执行时间。很简单,但是您应该将它们写在开始程序执行的主函数中。如果即使在遇到错误时也想获得执行时间,则将参数“开始”添加到该位置并进行计算,例如:
def sample_function(start,**kwargs):
try:
# Your statements
except:
# except statements run when your statements raise an exception
stop = timeit.default_timer()
execution_time = stop - start
print("Program executed in " + str(execution_time))
finally
一部分吗?
以下代码段以一种易于阅读的<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)
我已经看过timeit模块,但似乎只适用于小段代码。我想安排整个节目的时间。
$ python -mtimeit -n1 -r1 -t -s "from your_module import main" "main()"
它运行一次your_module.main()
功能,并使用以下命令打印经过的时间time.time()
功能作为计时器。
要/usr/bin/time
在Python中进行仿真,请参见带有/ usr / bin / time的Python子进程:如何捕获计时信息,但忽略所有其他输出?。
要测量time.sleep()
每个函数的CPU时间(例如,不包括中的时间),您可以使用profile
模块(cProfile
在Python 2上):
$ python3 -mprofile your_module.py
如果您想使用相同的计时器,则可以传递-p
到timeit
上面的命令profile
模块使用的。
我也喜欢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))
在单元格中,可以使用Jupyter的%%time
magic命令来测量执行时间:
%%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.
line_profiler将分析各个代码行执行所需的时间。剖析器通过Cython在C中实现,以减少分析的开销。
from line_profiler import LineProfiler
import random
def do_stuff(numbers):
s = sum(numbers)
l = [numbers[i]/43 for i in range(len(numbers))]
m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()
结果将是:
Timer unit: 1e-06 s
Total time: 0.000649 s
File: <ipython-input-2-2e060b054fea>
Function: do_stuff at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 def do_stuff(numbers):
5 1 10 10.0 1.5 s = sum(numbers)
6 1 186 186.0 28.7 l = [numbers[i]/43 for i in range(len(numbers))]
7 1 453 453.0 69.8 m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
我使用了一个非常简单的函数来计时部分代码执行时间:
import time
def timing():
start_time = time.time()
return lambda x: print("[{:.2f}s] {}".format(time.time() - start_time, x))
要使用它,只需在代码之前调用它以进行测量以检索函数计时,然后在代码后调用带有注释的函数。时间将显示在评论的前面。例如:
t = timing()
train = pd.read_csv('train.csv',
dtype={
'id': str,
'vendor_id': str,
'pickup_datetime': str,
'dropoff_datetime': str,
'passenger_count': int,
'pickup_longitude': np.float64,
'pickup_latitude': np.float64,
'dropoff_longitude': np.float64,
'dropoff_latitude': np.float64,
'store_and_fwd_flag': str,
'trip_duration': int,
},
parse_dates = ['pickup_datetime', 'dropoff_datetime'],
)
t("Loaded {} rows data from 'train'".format(len(train)))
然后输出将如下所示:
[9.35s] Loaded 1458644 rows data from 'train'
我在很多地方都遇到过同样的问题,所以我创建了一个便利包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
。
这是Paul McGuire的答案,对我有用。以防万一有人在运行那个困难。
import atexit
from time import clock
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value
def secondsToStr(t):
return "%d:%02d:%02d.%03d" % \
reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
[(t*1000,),1000,60,60])
line = "="*40
def log(s, elapsed=None):
print (line)
print (secondsToStr(clock()), '-', s)
if elapsed:
print ("Elapsed time:", elapsed)
print (line)
def endlog():
end = clock()
elapsed = end-start
log("End Program", secondsToStr(elapsed))
def now():
return secondsToStr(clock())
def main():
start = clock()
atexit.register(endlog)
log("Start Program")
timing.main()
导入文件后,从程序中调用。
稍后的答案,但我使用timeit
:
import timeit
code_to_test = """
a = range(100000)
b = []
for i in a:
b.append(i*2)
"""
elapsed_time = timeit.timeit(code_to_test, number=500)
print(elapsed_time)
# 10.159821493085474
code_to_test
。number
参数指定代码应重复的次数。Python程序执行时间的时间可能不一致,具体取决于:
这是因为最有效的方法是使用“增长顺序”并学习“ O”表示法来正确执行。
无论如何,您可以尝试使用以下简单算法以特定的机器每秒计数步骤来评估任何Python程序的性能: 使其适应您要评估的程序
import time
now = time.time()
future = now + 10
step = 4 # Why 4 steps? Because until here already four operations executed
while time.time() < future:
step += 3 # Why 3 again? Because a while loop executes one comparison and one plus equal statement
step += 4 # Why 3 more? Because one comparison starting while when time is over plus the final assignment of step + 1 and print statement
print(str(int(step / 10)) + " steps per second")
您只需在Python中执行此操作即可。无需使其变得复杂。
import time
start = time.localtime()
end = time.localtime()
"""Total execution time in seconds$ """
print(end.tm_sec - start.tm_sec)
首先,通过以管理员身份打开命令提示符(CMD)并在其中键入命令,以安装对人类友好的软件包-
pip install humanfriendly
码:
from humanfriendly import format_timespan
import time
begin_time = time.time()
# Put your code here
end_time = time.time() - begin_time
print("Total execution time: ", format_timespan(end_time))
输出:
要使用metakermit更新的Python 2.7 答案,您将需要单调包。
代码如下:
from datetime import timedelta
from monotonic import monotonic
start_time = monotonic()
end_time = monotonic()
print(timedelta(seconds=end_time - start_time))
我尝试使用以下脚本找到时差。
import time
start_time = time.perf_counter()
[main code here]
print (time.perf_counter() - start_time, "seconds")
如果要以微秒为单位测量时间,则可以使用以下版本,完全基于Paul McGuire和Nicojo的回答-这是Python 3代码。我还添加了一些颜色:
import atexit
from time import time
from datetime import timedelta, datetime
def seconds_to_str(elapsed=None):
if elapsed is None:
return datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
else:
return str(timedelta(seconds=elapsed))
def log(txt, elapsed=None):
colour_cyan = '\033[36m'
colour_reset = '\033[0;0;39m'
colour_red = '\033[31m'
print('\n ' + colour_cyan + ' [TIMING]> [' + seconds_to_str() + '] ----> ' + txt + '\n' + colour_reset)
if elapsed:
print("\n " + colour_red + " [TIMING]> Elapsed time ==> " + elapsed + "\n" + colour_reset)
def end_log():
end = time()
elapsed = end-start
log("End Program", seconds_to_str(elapsed))
start = time()
atexit.register(end_log)
log("Start Program")
log()=>函数,输出定时信息。
txt ==>要记录的第一个参数,以及用来标记时间的字符串。
atexit ==> Python模块,用于注册程序退出时可以调用的函数。