%timeit c = np.intersect1d(a, b)
%timeit result = np.array(set(a).intersection(b))
结论:
The slowest run took 10.96 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 5: 19.6 µs per loop
The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 5: 3.02 µs per loop
a = np.random.randint(100_000, size=500_000)
b = np.random.randint(100_000, size=500_000)
%timeit c = np.intersect1d(a, b)
%timeit result = np.array(set(a).intersection(b))
结论:
10 loops, best of 5: 119 ms per loop
1 loop, best of 5: 296 ms per loop
解释:
set(a)
从一个字段创建一个集合а
。intersection(b)
,这将使其与字段相交b
(也自动转换为集合)。对此有一个特殊的方法
Numpy
:使用数组
Numpy
时,最好使用它自己的方法,这通常比使用 pure 更有效率Python
。PS 很奇怪,但在 Google Colab 中发现纯 python 仍然更快。神秘)
结论:
PPS 啊,好吧,像往常一样,它的大小。在小型数组上,纯 python 更快。在非常大的阵列
Numpy
出租车上。结论: