对于python来说是新手,并且在速度至关重要的应用程序上工作-本质上是为了尽可能在接近7AM的时间在线进行预订。试图理解函数调用对执行速度的影响,所以我开发了一个非常简单的测试:计数到1,000,000,并同时使用内联函数和被调用函数来开始和结束时间。但是,我得到的结果表明,该函数大约花费了内联代码花费时间的56%。我一定真的很想念东西。谁能解释一下。
代码:
import time
def timeit():
temp = 0
while temp < 10000000:
temp += 1
return
time1 = time.time()
temp = 0
while temp < 10000000:
temp += 1
time2 = time.time()
timeit()
time3 = time.time()
temp = 0
while temp < 10000000:
temp += 1
time4 = time.time()
timeit()
time5 = time.time()
print("direct took " + "{:.16f}".format(time2 - time1) + " seconds")
print("indirect took " + "{:.16f}".format(time3 - time2) + " seconds")
print("2nd direct took " + "{:.16f}".format(time4 - time3) + " seconds")
print("2nd indirect took " + "{:.16f}".format(time5 - time4) + " seconds")
结果:
直接花费了1.6094279289245605秒
间接花费0.9220039844512939秒
第二个直接花费了1.5781939029693604秒
第二个间接花费了0.9375154972076416秒
我为错过一些我必须做的愚蠢的事情而道歉?