Closed
Description
Comparisons between numpy scalars and python scalars are very slow. It's an order of magnitude faster to do int(npint) == pyint
than npint == pyint
.
It seems to me that at worst be no slower as numpy could just do int()
for me?
The slowdown was apparent with all the types I tried including uint8, int32, float32 and uint64, but not int64 and float64, which are fast.
Reproducing code example
import numpy as np
import time
N = 1024 * 32
T = np.int32
def testPvP():
y = 0
x = T(y)
t0 = time.perf_counter()
for i in range(N):
int(x) == y
t = time.perf_counter() - t0
print('int(np) == py:', t * 1000, 'ms')
def testNvP():
y = 0
x = T(y)
t0 = time.perf_counter()
for i in range(N):
x == y
t = time.perf_counter() - t0
print(' np == py:', t * 1000, 'ms')
for i in range(5):
testPvP()
testNvP()
print()
int(np) == py: 7.214172000000005 ms
np == py: 50.713307000000015 ms
int(np) == py: 6.769899999999995 ms
np == py: 51.27685699999998 ms
int(np) == py: 6.717130000000015 ms
np == py: 55.45432100000003 ms
int(np) == py: 7.091216000000012 ms
np == py: 51.84300200000003 ms
int(np) == py: 6.472694000000001 ms
np == py: 55.38991200000004 ms
Numpy/Python version information:
1.17.0 3.7.4 (default, Jul 13 2019, 14:20:24)
[GCC 6.3.0 20170516]