我做了以下实验:
In [1]: import numpy as np
In [2]: l = range(5000000)
In [3]: a32 = np.array(l, dtype='uint32')
In [4]: a64 = np.array(l, dtype='uint64')
In [5]: %timeit l[101233]
The slowest run took 39.00 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 30.6 ns per loop
In [6]: %timeit a32[101233]
The slowest run took 281.55 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 88.9 ns per loop
In [7]: %timeit a64[101233]
The slowest run took 22192.85 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 85.9 ns per loop
我希望numpy数组中的索引元素应该与Python列表中的速度大致相同。
但事实证明它的速度慢了两倍以上,性能非常不稳定(见a64[101233]
的结果,最慢的运行时间长了22192.85倍......)。
这种行为可能是什么原因?