所以在Java语言中,我们可以做How to measure time taken by a function to execute
但是在python中是如何做到的呢?测量代码行之间的时间开始和结束时间?这样做的东西:
import some_time_library
starttime = some_time_library.some_module()
code_tobe_measured()
endtime = some_time_library.some_module()
time_taken = endtime - starttime发布于 2013-01-22 13:34:22
如果您想测量CPU时间,可以使用Python3.3及以上版本的time.process_time():
import time
start = time.process_time()
# your code here
print(time.process_time() - start)第一个调用会打开计时器,第二个调用会告诉你已经过了多少秒。
还有一个函数time.clock(),但它是deprecated since Python 3.3,将在Python3.8中删除。
有更好的性能分析工具,如timeit和profile,但是time.process_time()将测量CPU时间,这就是您所询问的。
如果您想测量挂钟时间,请使用time.time()。
https://stackoverflow.com/questions/14452145
复制相似问题