This question already has an answer here:
- Python Numpy Data Types Performance 2 answers
In my benchmark using numpy 1.12.0, calculating dot products with float32
ndarrays
is much faster than the other data types:
In [3]: f16 = np.random.random((500000, 128)).astype('float16')
In [4]: f32 = np.random.random((500000, 128)).astype('float32')
In [5]: uint = np.random.randint(1, 60000, (500000, 128)).astype('uint16')
In [7]: %timeit np.einsum('ij,ij->i', f16, f16)
1 loop, best of 3: 320 ms per loop
In [8]: %timeit np.einsum('ij,ij->i', f32, f32)
The slowest run took 4.88 times longer than the fastest. This could mean that an intermediate result is being cached.
10 loops, best of 3: 19 ms per loop
In [9]: %timeit np.einsum('ij,ij->i', uint, uint)
10 loops, best of 3: 43.5 ms per loop
I've tried profiling einsum
, but it just delegates all the computing to a C function, so I don't know what's the main reason for this performance difference.