Python 与向量长度无关的Numpy性能

Python 与向量长度无关的Numpy性能,python,performance,numpy,Python,Performance,Numpy,我对numpy的表演有点问题。许多操作的性能似乎几乎完全独立于向量长度: In [23]: y1 = np.random.randn(3) In [24]: y2 = np.random.randn(300) In [25]: %timeit np.mean(y1) The slowest run took 9.65 times longer than the fastest. This could mean that an intermediate result is being cach

我对numpy的表演有点问题。许多操作的性能似乎几乎完全独立于向量长度:

In [23]: y1 = np.random.randn(3)

In [24]: y2 = np.random.randn(300)

In [25]: %timeit np.mean(y1)
The slowest run took 9.65 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 4.91 µs per loop

In [26]: %timeit np.mean(y2)
The slowest run took 9.58 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.07 µs per loop

In [27]: %timeit np.median(y1)
The slowest run took 5.28 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 23.6 µs per loop

In [28]: %timeit np.median(y2)
The slowest run took 4.91 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 25.7 µs per loop

In [29]: %timeit np.max(y1)
The slowest run took 19.66 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.91 µs per loop

In [30]: %timeit np.max(y2)
The slowest run took 19.95 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.98 µs per loop

它有什么毛病吗

我想问题是这两个列表都非常小,开销来自其他地方,而不是简单地查找平均值、中位数和最大值。要想看到差异,尝试一个大的listNumpy是非常有效的,因为它几乎是本地运行的。你不会在几百个条目上看到巨大的差异。尝试创建1000、10000、100000和1000000中的一个,并可能使用PLT绘制结果以查看时间的进展。将这些时间与列表上的等效计算进行比较,例如
sum(y1l)/len(y1l)
max(y1l)
。数组操作的开销掩盖了小规模的每元素变化。@Goodies如果不是因为开销太大,我可以得到它。25.7我们很难找到小名单的中位数。我原以为大矢量运行1us,小矢量运行1us,纳秒级。编辑:好的,低纳秒是期望有点多,但一对夫妇的100纳秒。