[英]Multiplying array in python
From this question I see how to multiply a whole numpy array with the same number (second answer, by JoshAdel). 从这个问题我看到如何将整个numpy数组乘以相同的数字(第二个答案,JoshAdel)。 But when I change P into the maximum of a (long) array, is it better to store the maximum on beforehand, or does it calculate the maximum of H just once in the second example?
但是当我将P改变为(长)数组的最大值时,最好是事先存储最大值,还是在第二个例子中只计算一次H的最大值?
import numpy as np
H = [12,12,5,32,6,0.5]
P=H.max()
S=[22, 33, 45.6, 21.6, 51.8]
SP = P*np.array(S)
or 要么
import numpy as np
H = [12,12,5,32,6,0.5]
S=[22, 33, 45.6, 21.6, 51.8]
SP = H.max()*np.array(S)
So does it calculate H.max()
for every item it has to multiply, or is it smart enough to it just once? 那么它是否计算了它必须乘以的每个项目的
H.max()
,或者它只是一次足够聪明? In my code S
and H
are longer arrays then in the example. 在我的代码中,
S
和H
是较长的数组,然后在示例中。
There is little difference between the 2 methods: 两种方法之间几乎没有区别:
In [74]:
import numpy as np
H = np.random.random(100000)
%timeit P=H.max()
S=np.random.random(100000)
%timeit SP = P*np.array(S)
%timeit SP = H.max()*np.array(S)
10000 loops, best of 3: 51.2 µs per loop
10000 loops, best of 3: 165 µs per loop
1000 loops, best of 3: 217 µs per loop
Here you can see that the individual step of pre-calculating H.max()
is no different from calculating it in a single line 在这里你可以看到预先计算
H.max()
的单个步骤与在单行中计算它没有什么不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.