gpt4 book ai didi

python - 为什么 x**3 比 x*x*x 慢?

转载 作者:IT老高 更新时间:2023-10-28 22:21:32 31 4
gpt4 key购买 nike

在 NumPy 中,x*x*x 比 x**3 甚至 np.power(x, 3) 快一个数量级。

x = np.random.rand(1e6)
%timeit x**3
100 loops, best of 3: 7.07 ms per loop

%timeit x*x*x
10000 loops, best of 3: 163 µs per loop

%timeit np.power(x, 3)
100 loops, best of 3: 7.15 ms per loop

关于为什么会发生这种行为的任何想法?据我所知,这三个产生相同的输出(用 np.allclose 检查)。

最佳答案

根据 this answer ,这是因为求幂的实现有一些乘法没有的开销。然而,随着指数的增加,朴素的乘法会变得越来越慢。实证论证:

 In [3]: x = np.random.rand(1e6)

In [15]: %timeit x**2
100 loops, best of 3: 11.9 ms per loop

In [16]: %timeit x*x
100 loops, best of 3: 12.7 ms per loop

In [17]: %timeit x**3
10 loops, best of 3: 132 ms per loop

In [18]: %timeit x*x*x
10 loops, best of 3: 27.2 ms per loop

In [19]: %timeit x**4
10 loops, best of 3: 132 ms per loop

In [20]: %timeit x*x*x*x
10 loops, best of 3: 42.4 ms per loop

In [21]: %timeit x**10
10 loops, best of 3: 132 ms per loop

In [22]: %timeit x*x*x*x*x*x*x*x*x*x
10 loops, best of 3: 137 ms per loop

In [24]: %timeit x**15
10 loops, best of 3: 132 ms per loop

In [25]: %timeit x*x*x*x*x*x*x*x*x*x*x*x*x*x*x
1 loops, best of 3: 212 ms per loop

请注意,求幂时间或多或少保持不变,除了我怀疑是特殊情况的 x**2 情况,而乘法变得越来越慢。看来您可以利用它来获得更快的整数求幂...例如:

In [26]: %timeit x**16
10 loops, best of 3: 132 ms per loop

In [27]: %timeit x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x
1 loops, best of 3: 225 ms per loop

In [28]: def tosixteenth(x):
....: x2 = x*x
....: x4 = x2*x2
....: x8 = x4*x4
....: x16 = x8*x8
....: return x16
....:

In [29]: %timeit tosixteenth(x)
10 loops, best of 3: 49.5 ms per loop

您似乎可以通过将任何整数拆分为 2 的幂和,计算上述 2 的每个幂,然后求和来通用地应用此技术:

In [93]: %paste
def smartintexp(x, exp):
result = np.ones(len(x))
curexp = np.array(x)
while True:
if exp%2 == 1:
result *= curexp
exp >>= 1
if not exp: break
curexp *= curexp
return result
## -- End pasted text --

In [94]: x
Out[94]:
array([ 0.0163407 , 0.57694587, 0.47336487, ..., 0.70255032,
0.62043303, 0.0796748 ])

In [99]: x**21
Out[99]:
array([ 3.01080670e-38, 9.63466181e-06, 1.51048544e-07, ...,
6.02873388e-04, 4.43193256e-05, 8.46721060e-24])

In [100]: smartintexp(x, 21)
Out[100]:
array([ 3.01080670e-38, 9.63466181e-06, 1.51048544e-07, ...,
6.02873388e-04, 4.43193256e-05, 8.46721060e-24])

In [101]: %timeit x**21
10 loops, best of 3: 132 ms per loop

In [102]: %timeit smartintexp(x, 21)
10 loops, best of 3: 70.7 ms per loop

对于小的偶数次方来说很快:

In [106]: %timeit x**32
10 loops, best of 3: 131 ms per loop

In [107]: %timeit smartintexp(x, 32)
10 loops, best of 3: 57.4 ms per loop

但随着指数变大变慢:

In [97]: %timeit x**63
10 loops, best of 3: 133 ms per loop

In [98]: %timeit smartintexp(x, 63)
10 loops, best of 3: 110 ms per loop

对于大的最坏情况并不会更快:

In [115]: %timeit x**511
10 loops, best of 3: 135 ms per loop

In [114]: %timeit smartintexp(x, 511)
10 loops, best of 3: 192 ms per loop

关于python - 为什么 x**3 比 x*x*x 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18453771/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com