我将一个整数添加到包含numpy.int64
0个元素的整数数组中.当我第一次将单个整数从numpy.int64
转换为python本机int
时,速度加快了25%.
为什么?作为一般的经验法则,我是否应该将单个数字转换为本机python格式,以用于具有大约这个大小的数组的单个数字到数组操作?
注:可能与我之前的问题Conjugating a complex number much faster if number has python-native complex type有关.
import numpy as np
nnu = 10418
nnu_use = 5210
a = np.random.randint(nnu,size=1000)
b = np.random.randint(nnu_use,size=1)[0]
%timeit a + b # --> 3.9 µs ± 19.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit a + int(b) # --> 2.87 µs ± 8.07 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
请注意,对于标量到标量的操作,速度可能会非常快(factor 50),如下所示:
np.random.seed(100)
a = (np.random.rand(1))[0]
a_native = float(a)
b = complex(np.random.rand(1)+1j*np.random.rand(1))
c = (np.random.rand(1)+1j*np.random.rand(1))[0]
c_native = complex(c)
%timeit a * (b - b.conjugate() * c) # 6.48 µs ± 49.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit a_native * (b - b.conjugate() * c_native) # 283 ns ± 7.78 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit a * b # 5.07 µs ± 17.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit a_native * b # 94.5 ns ± 0.868 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Update:是不是最新的numpy版本修复了速度差异?numpy 1.23
的发行说明提到标量运算现在快得多,请参见https://numpy.org/devdocs/release/1.23.0-notes.html#performance-improvements-and-changes和https://github.com/numpy/numpy/pull/21188.我用python 3.7.6, numpy 1.21.2
.