为什么大量分解的速度更快 [英] Why does division become faster with very large numbers

查看:161
本文介绍了为什么大量分解的速度更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看除法运算的常数,所以我运行了这段代码

 导入时间

def di(n):
n / 101

i = 10
而我< 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000:
start = time.clock()
di(i)
end = time.clock()
print( On + str(i)+ + str(end-start))
i * = 10000

这是我得到的输出

 在10 5.98714047756e-06 
在100000 4.7041818038e-06
在1000000000 2.56591734753e-06
上10000000000000 2.99357023878e-06
上100000000000000000 2.99357023878e-06
上1000000000000000000000 2.99357023879e-06
上10000000000000000000000000 2.99357023878e-06
上100000000000000000000000000000 3.42122313003e-06
在1000000000000000000000000000000000000000 3.42122313003e-06
在10000000000000000000000000000000000000000000 3.84887602128e-06
在1000000000000000000000000000000000000000000000000000 3.84887602128e-06
在10000000000000000000000000000000000000000000 3.84887602128e-06
在10000000000000000000000000000000000000000000000000000000 1 .71061156501e-06
上100000000000000000000000000000000000000000000000000000000000000000 1.71061156502e-06
上1000000000000000000000000000000000000000000000000000000000 1.71061156501e-06
上10000000000000000000000000000000000000000000000000000000000000000000 2.13826445628e-06
上100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b 2.13826445628e-06
在10000000000000000000000000000000000000000000000000000000000000000000000000000000000000 2.13826445628e-06
在100000000000000000000000000000000000000000000000000000000000000000000000000000000000 2.13826445626e-06
在1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 2.13826445626e-06
p>

为什么后来的除法通常会比前两个术语中最小的时间更快?

解决方案


为什么相对于前两个术语中最小的时间,该除法通常会在以后变得更快吗?


,实际上。如果我将 di 替换为:

  def di(n):
for i在范围(10000000)中:n / 101

然后我得到了(Python 3.5,我假设您正在使用):

 在10 0.546889 
在100000 0.545004
在1000000000 0.5454929999999998
在10000000000000上0.5519709999999998
在100000000000000000 1.330797
在1000000000000000000000 1.31053
在10000000000000000000000000 1.3393129999999998
在100000000000000000000000000000 1.3524339999999997
在1000000000000000000000000000000000 1.3817269999999997
在1000000000000000000000000 $ b在100000000000000000000000000000000000000000000 1.3358929999999987
在1000000000000000000000000000000000000000000000 1.3773859999999996
在10000000000000000000000000000000000000000000000000000000 1.3326890000000002
在100000000000000000000000000000000000000000000000000000000000000 3000 1.3704769999999993
在100000000000000000000 0000000000000000000000000000000000000 1.3235019999999995
在10000000000000000000000000000000000000000000000000000000000000 1.357647
在100000000000000000000000000000000000000000000000000000000000000000 1.3341190000000012
在1000000000000000000000000000000000000000000000000000000000000000000000 1.326544000000002
在10000000000000000000000000000000000000000000000000000000000000000000000000 1.3671139999999973
在100000000000000000000000000000000000000000000000000000000000000000000000000000 1.3630120000000012
在1000000000000000000000000000000000000000000000000000000000000000000000000000000000 1.3600200000000022
在10000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1.3189189999999975
在100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1.3503469999999993

您可以看到,大约有两次:较小的数字,较大的一个数字。在Python 3.5中, / 进行浮点除法,因此无论数字大小如何,它实际上应该花费相同的时间。



但是,从小到大都有差距。使用以下函数保留语义的Python 2.7也会发生相同的结果:

  def di(n):
用于我在xrange(10000000)中:n / 101.0

在同一台机器上,我得到:

 在10 0.617427 
在100000 0.61805
在1000000000 0.6366
在10000000000000 0.620919
在100000000000000000 0.616695
上1000000000000000000000 0.927353
上10000000000000000000000000 1.007156
上100000000000000000000000000000 0.98597
上1000000000000000000000000000000000 0.99258
上10000000000000000000000000000000000000000000 0.966753
上10000000000000000000000000000000000000000 0.992684
上0.991711
上10000000000000000000000000000000000000000000000000 0.994703
上100000000000000000000000000000000000000000000000000000000000 0.978877
上1000000000000000000000000000000000000000000000000000000000000000000000 0.982035
上10000000000000000 000000000000000000000000000000000000000000000 0.973266
在100000000000000000000000000000000000000000000000000000000000000000 0.977911
在1000000000000000000000000000000000000000000000000000000000000000000000 0.996857
在10000000000000000000000000000000000000000000000000000000000000000000000000 0.972555
在100000000000000000000000000000000000000000000000000000000000000000000000000000 0.985676
在1000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.987412
在10000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.997207
在100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.970129

这与将整数转换为浮点数有关(具体细节待定)数。当整数超过某个点时,速度会变慢,并且当超过该点时,除法运算会花费更长的时间。


I wanted to see how constant the division operation is, so I ran this code

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

And this was the output I got

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

Why does the division generally get faster later on as opposed to the time of the first two terms which are the smallest?

解决方案

Why does the division generally get faster later on as opposed to the time of the first two terms which are the smallest?

It doesn't, actually. If I replace di with:

def di(n):
    for i in range(10000000): n / 101

Then I get (Python 3.5, which I presume you are using):

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

As you can see, there are roughly two times: one for smaller numbers, and one for larger numbers. In Python 3.5, / does floating point division, so it should actually take about the same amount of time regardless of the size of the numbers.

However, there is that gap from small to large. The same result happens with Python 2.7 using the following function to preserve semantics:

def di(n):
    for i in xrange(10000000): n / 101.0

On the same machine, I get:

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

This has something to do (exact specifics to be determined) with converting the integer to a floating point number. This is slower with integers beyond a certain point, and when that point is crossed, the division starts taking longer.

这篇关于为什么大量分解的速度更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆