import time
def execution_time(code):
start = time.clock(code)
result = eval(code)
time = time.clock(code) - start
python中估算程序运行时长的代码,非常简洁。 根据运行结果,简单的加法循环,1亿次耗时5秒左右。
本文介绍了一种在Python中简单而有效的测量代码执行时间的方法。通过使用内置的时间模块,可以方便地记录并计算出任意代码片段的运行时长。实测显示,一个包含一亿次加法操作的循环耗时大约为5秒。
import time
def execution_time(code):
start = time.clock(code)
result = eval(code)
time = time.clock(code) - start
python中估算程序运行时长的代码,非常简洁。 根据运行结果,简单的加法循环,1亿次耗时5秒左右。
500
359
2083
275

被折叠的 条评论
为什么被折叠?