- How does
- How does Python’s in-built function
- What works better for lists?
- What works better for
Before we get into the performance of it all, let us see how these work:
sumiterates over the iterable (in this case the list or array) and adds all elements.
summethod iterates over the stored C array and adds these C values and finally wraps that value in a Python type (in this case
numpy.int64) and returns it.
sumfunction converts the input to an
array(at least if it isn’t an array already) and then uses the NumPy
Now look at the graphs below. The first graph is the comparison between
np.sum() and Python’s
sum() when it comes to lists and the second one is about arrays.
Python’s sum is faster on lists, while NumPys sum is faster on arrays. But why does that happen?
It is to be noted that adding C values from a C array is much faster than adding Python objects, which is why the NumPy functions can be much faster.
But converting a Python list to a NumPy array is relatively slow and then you still have to add the C values which is why for lists the Python’s
sum will be faster.
Also, why is Python’s
sum on an
array so slow. This has to do with the fact that Python’s sum simply iterates over whatever you pass in. In case of a list it gets the stored Python object but in case of a 1D NumPy array there are no stored Python objects, just C values, so Python & NumPy have to create a Python object (an
numpy.int64) for each element and then these Python objects have to be added. The creating the wrapper for the C value is what makes it really slow.
This posts differentiates between the two in terms of their working and their performance when it comes to lists and arrays. Do try the same out with different objects such as tuples and ranges.