What is the difference between np.sum() and sum()?

  • How does numpy.sum() work?
  • How does Python’s in-built function sum() work?
  • What works better for lists?
  • What works better for numpy.ndarray or arrays?

Before we get into the performance of it all, let us see how these work:

  • Pythons sum iterates over the iterable (in this case the list or array) and adds all elements.
  • NumPys sum method iterates over the stored C array and adds these C values and finally wraps that value in a Python type (in this case numpy.int32 (or numpy.int64) and returns it.
  • NumPys sum function converts the input to an array (at least if it isn’t an array already) and then uses the NumPy sum method.

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.int32 or 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.