我想知道除法运算是多么恒定,所以我运行了这段代码
import time
def di(n):
n/101
i = 10
while i < 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:
start = time.clock()
di(i)
end = time.clock()
print("On " + str(i) + " " + str(end-start))
i *= 10000
这是我得到的输出
On 10 5.98714047756e-06
On 100000 4.7041818038e-06
On 1000000000 2.56591734753e-06
On 10000000000000 2.99357023878e-06
On 100000000000000000 2.99357023878e-06
On 1000000000000000000000 2.99357023879e-06
On 10000000000000000000000000 2.99357023878e-06
On 100000000000000000000000000000 3.42122313003e-06
On 1000000000000000000000000000000000 3.42122313003e-06
On 10000000000000000000000000000000000000 3.84887602128e-06
On 100000000000000000000000000000000000000000 3.42122313003e-06
On 1000000000000000000000000000000000000000000000 3.84887602128e-06
On 10000000000000000000000000000000000000000000000000 1.71061156501e-06
On 100000000000000000000000000000000000000000000000000000 1.71061156502e-06
On 1000000000000000000000000000000000000000000000000000000000 1.71061156501e-06
On 10000000000000000000000000000000000000000000000000000000000000 2.13826445628e-06
On 100000000000000000000000000000000000000000000000000000000000000000 1.71061156502e-06
On 1000000000000000000000000000000000000000000000000000000000000000000000 2.13826445628e-06
On 10000000000000000000000000000000000000000000000000000000000000000000000000 2.13826445628e-06
On 100000000000000000000000000000000000000000000000000000000000000000000000000000 2.13826445626e-06
On 1000000000000000000000000000000000000000000000000000000000000000000000000000000000 2.13826445626e-06
为什么除了前两个最小的时间之外,除法通常会更快?
答案 0 :(得分:3)
为什么除了前两个最小的时间之外,除法通常会更快?
实际上并非如此。如果我将di
替换为:
def di(n):
for i in range(10000000): n / 101
然后我得到(Python 3.5,我认为你正在使用):
On 10 0.546889
On 100000 0.545004
On 1000000000 0.5454929999999998
On 10000000000000 0.5519709999999998
On 100000000000000000 1.330797
On 1000000000000000000000 1.31053
On 10000000000000000000000000 1.3393129999999998
On 100000000000000000000000000000 1.3524339999999997
On 1000000000000000000000000000000000 1.3817269999999997
On 10000000000000000000000000000000000000 1.3412670000000002
On 100000000000000000000000000000000000000000 1.3358929999999987
On 1000000000000000000000000000000000000000000000 1.3773859999999996
On 10000000000000000000000000000000000000000000000000 1.3326890000000002
On 100000000000000000000000000000000000000000000000000000 1.3704769999999993
On 1000000000000000000000000000000000000000000000000000000000 1.3235019999999995
On 10000000000000000000000000000000000000000000000000000000000000 1.357647
On 100000000000000000000000000000000000000000000000000000000000000000 1.3341190000000012
On 1000000000000000000000000000000000000000000000000000000000000000000000 1.326544000000002
On 10000000000000000000000000000000000000000000000000000000000000000000000000 1.3671139999999973
On 100000000000000000000000000000000000000000000000000000000000000000000000000000 1.3630120000000012
On 1000000000000000000000000000000000000000000000000000000000000000000000000000000000 1.3600200000000022
On 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1.3189189999999975
On 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1.3503469999999993
正如您所看到的,大概有两次:一次是较小的数字,另一次是较大的数字。在Python 3.5中,/
执行浮点除法,因此无论数字的大小如何,它实际上应该花费大约相同的时间。
然而,从小到大存在差距。使用以下函数来保留语义时,Python 2.7也会出现相同的结果:
def di(n):
for i in xrange(10000000): n / 101.0
在同一台机器上,我得到:
On 10 0.617427
On 100000 0.61805
On 1000000000 0.6366
On 10000000000000 0.620919
On 100000000000000000 0.616695
On 1000000000000000000000 0.927353
On 10000000000000000000000000 1.007156
On 100000000000000000000000000000 0.98597
On 1000000000000000000000000000000000 0.99258
On 10000000000000000000000000000000000000 0.966753
On 100000000000000000000000000000000000000000 0.992684
On 1000000000000000000000000000000000000000000000 0.991711
On 10000000000000000000000000000000000000000000000000 0.994703
On 100000000000000000000000000000000000000000000000000000 0.978877
On 1000000000000000000000000000000000000000000000000000000000 0.982035
On 10000000000000000000000000000000000000000000000000000000000000 0.973266
On 100000000000000000000000000000000000000000000000000000000000000000 0.977911
On 1000000000000000000000000000000000000000000000000000000000000000000000 0.996857
On 10000000000000000000000000000000000000000000000000000000000000000000000000 0.972555
On 100000000000000000000000000000000000000000000000000000000000000000000000000000 0.985676
On 1000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.987412
On 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.997207
On 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.970129
通过将整数转换为浮点数,可以做一些事情(确切的具体细节)。对于超过某一点的整数,这会更慢,当超过该点时,除法开始花费更长的时间。
答案 1 :(得分:1)
“分区通常变快”并不能准确描述您的观察结果。看起来它们变得更快,然后更慢,然后更快,然后更慢,然后更快,然后更慢,然后更快。我对你的脚本做了两次修改:
di
一次,而是将其调用10
次并打印出最小,最大和平均时间。我发现最大时间大致显示了你看到的模式,并且最小时间是0.0,所以解释器可能会将函数调用的结果缓存为优化。i
的大数字开始,每次i /= 10000
下降。最大时间在这里显示出类似的模式,因为前几次计算花费的时间比其余的更多。也就是说,无论进行什么计算似乎都需要花费更多时间,无论是从小数字开始的程序,还是以大数字开头的程序都无关紧要。