I recently came across a great SO post in which a user suggests that numpy.sum
is faster than Python's sum
when it comes to dealing with NumPy arrays.
This made me think, are element-wise operations on NumPy arrays faster with NumPy functions than operators? If so, then why is this the case?
Consider the following example.
import numpy as np
a = np.random.random(1e10)
b = np.random.random(1e10)
Will np.subtract(a, b)
be reliably faster than a - b
?
Not really. You can check the timings pretty easily though.
a = np.random.normal(size=1000)
b = np.random.normal(size=1000)
%timeit np.subtract(a, b)
# 1000000 loops, best of 3: 1.57 µs per loop
%timeit a - b
# 1000000 loops, best of 3: 1.47 µs per loop
%timeit np.divide(a, b)
# 100000 loops, best of 3: 3.51 µs per loop
%timeit a / b
# 100000 loops, best of 3: 3.38 µs per loop
The numpy functions actually seem to be a tad slower. I'm not sure if that's significant, but I suspect it might be because of some additional function call overhead on top of the same implementation.
EDIT: As @unutbu notes, it's probably because np.add
and friends have additional type-checking overhead to convert array-likes to arrays when necessary, so stuff like np.add([1, 2], [3, 4])
works.
No, not in a significant way.
The reason np.sum
is faster than sum
is that sum
is implemented to "naively" iterate over the iterable (in this case, the numpy array), calling elements' __add__
operator (which imposes a significant overhead), while numpy's implementation of sum
is optimized, e.g. taking advantage of the fact it knows the type (dtype) of the elements, and that they are contiguous in memory.
This is not the case with np.subtract(arr1, arr2)
and arr1-arr2
. The latter roughly translates to the former.
The difference arises from the fact one can override the subtraction operator in python, so numpy arrays override it to use the optimized version. The sum
operation, however, is not overridable, so numpy provides an alternative optimized version of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With