【发布时间】:2020-07-28 09:29:51
【问题描述】:
我使用timeit 模块来衡量我的代码的一些执行情况,在检查不同的脚本时,我发现“相同的脚本”有很大的不同,但以不同的方式定义。
代码.py 导入时间
code = '''
def count():
for i in range(100):
pass
'''
def count():
for i in range(100):
pass
print(timeit.timeit(code))
print(timeit.timeit(count))
输出:
0.17939981000017724
3.7566073690004487
幕后究竟发生了什么?我的意思是,在这两种情况下,这段代码是完全一样的,但是执行的时间差异很大。
【问题讨论】:
-
字符串
code中的源代码只是定义了一个函数,它实际上并没有运行这个函数。
标签: python performance time timeit