import time
start_time = time.time()
# your script
elapsed_time = time.time() - start_time
time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
将打印出来,持续2秒:
'00:00:02'
7分钟一秒:
'00:07:01'
注意,gmtime的最小时间单位是秒。如果需要微秒,请考虑以下内容:
import datetime
start = datetime.datetime.now()
# some code
end = datetime.datetime.now()
elapsed = end - start
print(elapsed)
# or
print(elapsed.seconds,":",elapsed.microseconds)
对于需要更好格式的用户
将打印出来,持续2秒:
7分钟一秒:
注意,gmtime的最小时间单位是秒。如果需要微秒,请考虑以下内容:
标准时间documentation
您还可以编写简单的decorator来简化对各种函数执行时间的度量:
用法:
您可以同时分析多个函数。然后要打印测量值,只需调用print_prof_data():
time.time()
将完成这项工作。你可能想看看this问题,但我认为没有必要。
相关问题 更多 >
编程相关推荐