🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-sum-in-python
numpy.sum() in Python - GeeksforGeeks
January 30, 2026 - numpy.sum() is a NumPy function used to calculate the sum of array elements. It can sum values across the entire array or along a specific axis.
🌐
DataCamp
datacamp.com › doc › numpy › sum
NumPy sum()
NumPy's `sum()` function is a powerful tool for array computation and analysis, allowing users to efficiently compute the sum of array elements along a specified axis.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.sum.html
numpy.ndarray.sum — NumPy v2.2 Manual
Return the sum of the array elements over the given axis. Refer to numpy.sum for full documentation.
🌐
Medium
medium.com › data-science › understanding-numpy-sum-1587eec69527
Understanding NumPy sum. If you are not clear on what NumPy is… | by Kshitij Bajracharya | TDS Archive | Medium
August 20, 2018 - Now if we look back at the statement from the docs, “we can sum each row of an array, in which case we operate along columns, or axis 1”, I think it makes a lot more sense. So, although we calculated the sum of each row, technically it is a column-wise addition rather than a row-wise addition as axis=0 is row and axis=1 is column. Why did NumPy not make it straightforward as in case of pandas?
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matrix.sum.html
numpy.matrix.sum — NumPy v2.1 Manual
Returns the sum of the matrix elements, along the given axis. Refer to numpy.sum for full documentation.
🌐
Medium
medium.com › @whyamit101 › understanding-numpy-sum-with-axis-parameter-1fc543fe9fa2
Understanding numpy.sum() with Axis Parameter | by why amit | Medium
February 9, 2025 - This calculates the sum of rows and columns, leaving only the sum of the elements in the last axis. If the concept feels tricky, think of it as “zooming out” on the array and combining dimensions.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Sum, mean, max, min for entire array, column/row-wise | note.nkmk.me
January 20, 2024 - Passing the array (ndarray) to np.sum() returns the sum of all elements.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.sum.html
numpy.ndarray.sum — NumPy v2.1 Manual
Return the sum of the array elements over the given axis. Refer to numpy.sum for full documentation.
Top answer
1 of 6
88

I got curious and timed it. numpy.sum seems much faster for numpy arrays, but much slower on lists.

import numpy as np
import timeit

x = range(1000)
# or 
#x = np.random.standard_normal(1000)

def pure_sum():
    return sum(x)

def numpy_sum():
    return np.sum(x)

n = 10000

t1 = timeit.timeit(pure_sum, number = n)
print 'Pure Python Sum:', t1
t2 = timeit.timeit(numpy_sum, number = n)
print 'Numpy Sum:', t2

Result when x = range(1000):

Pure Python Sum: 0.445913167735
Numpy Sum: 8.54926219673

Result when x = np.random.standard_normal(1000):

Pure Python Sum: 12.1442425643
Numpy Sum: 0.303303771848

I am using Python 2.7.2 and Numpy 1.6.1

2 of 6
87

[...] my [...] question here is would using numpy.sum on a list of Python integers be any faster than using Python's own sum?

The answer to this question is: No.

Pythons sum will be faster on lists, while NumPys sum will be faster on arrays. I actually did a benchmark to show the timings (Python 3.6, NumPy 1.14):

import random
import numpy as np
import matplotlib.pyplot as plt

from simple_benchmark import benchmark

%matplotlib notebook

def numpy_sum(it):
    return np.sum(it)

def python_sum(it):
    return sum(it)

def numpy_sum_method(arr):
    return arr.sum()

b_array = benchmark(
    [numpy_sum, numpy_sum_method, python_sum],
    arguments={2**i: np.random.randint(0, 10, 2**i) for i in range(2, 21)},
    argument_name='array size',
    function_aliases={numpy_sum: 'numpy.sum(<array>)', numpy_sum_method: '<array>.sum()', python_sum: "sum(<array>)"}
)

b_list = benchmark(
    [numpy_sum, python_sum],
    arguments={2**i: [random.randint(0, 10) for _ in range(2**i)] for i in range(2, 21)},
    argument_name='list size',
    function_aliases={numpy_sum: 'numpy.sum(<list>)', python_sum: "sum(<list>)"}
)

With these results:

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
b_array.plot(ax=ax1)
b_list.plot(ax=ax2)

Left: on a NumPy array; Right: on a Python list. Note that this is a log-log plot because the benchmark covers a very wide range of values. However for qualitative results: Lower means better.

Which shows that for lists Pythons sum is always faster while np.sum or the sum method on the array will be faster (except for very short arrays where Pythons sum is faster).

Just in case you're interested in comparing these against each other I also made a plot including all of them:

f, ax = plt.subplots(1)
b_array.plot(ax=ax)
b_list.plot(ax=ax)
ax.grid(which='both')

Interestingly the point at which numpy can compete on arrays with Python and lists is roughly at around 200 elements! Note that this number may depend on a lot of factors, such as Python/NumPy version, ... Don't take it too literally.

What hasn't been mentioned is the reason for this difference (I mean the large scale difference not the difference for short lists/arrays where the functions simply have different constant overhead). Assuming CPython a Python list is a wrapper around a C (the language C) array of pointers to Python objects (in this case Python integers). These integers can be seen as wrappers around a C integer (not actually correct because Python integers can be arbitrarily big so it cannot simply use one C integer but it's close enough).

For example a list like [1, 2, 3] would be (schematically, I left out a few details) stored like this:

A NumPy array however is a wrapper around a C array containing C values (in this case int or long depending on 32 or 64bit and depending on the operating system).

So a NumPy array like np.array([1, 2, 3]) would look like this:

The next thing to understand is how these functions 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.

Clearly adding C values from a C array is much faster than adding Python objects, which is why the NumPy functions can be much faster (see the second plot above, the NumPy functions on arrays beat the Python sum by far for large arrays).

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 sum will be faster.

The only remaining open question is why is Pythons sum on an array so slow (it's the slowest of all compared functions). And that actually has to do with the fact that Pythons 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.

Additionally, what are the implications (including performance) of using a Python integer versus a scalar numpy.int32? For example, for a += 1, is there a behavior or performance difference if the type of a is a Python integer or a numpy.int32?

I made some tests and for addition and subtractions of scalars you should definitely stick with Python integers. Even though there could be some caching going on which means that the following tests might not be totally representative:

from itertools import repeat

python_integer = 1000
numpy_integer_32 = np.int32(1000)
numpy_integer_64 = np.int64(1000)

def repeatedly_add_one(val):
    for _ in repeat(None, 100000):
        _ = val + 1

%timeit repeatedly_add_one(python_integer)
3.7 ms ± 71.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit repeatedly_add_one(numpy_integer_32)
14.3 ms ± 162 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit repeatedly_add_one(numpy_integer_64)
18.5 ms ± 494 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)


def repeatedly_sub_one(val):
    for _ in repeat(None, 100000):
        _ = val - 1

%timeit repeatedly_sub_one(python_integer)
3.75 ms ± 236 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit repeatedly_sub_one(numpy_integer_32)
15.7 ms ± 437 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit repeatedly_sub_one(numpy_integer_64)
19 ms ± 834 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

It's 3-6 times faster to do scalar operations with Python integers than with NumPy scalars. I haven't checked why that's the case but my guess is that NumPy scalars are rarely used and probably not optimized for performance.

The difference becomes a bit less if you actually perform arithmetic operations where both operands are numpy scalars:

def repeatedly_add_one(val):
    one = type(val)(1)  # create a 1 with the same type as the input
    for _ in repeat(None, 100000):
        _ = val + one

%timeit repeatedly_add_one(python_integer)
3.88 ms ± 273 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit repeatedly_add_one(numpy_integer_32)
6.12 ms ± 324 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit repeatedly_add_one(numpy_integer_64)
6.49 ms ± 265 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Then it's only 2 times slower.


In case you wondered why I used itertools.repeat here when I could simply have used for _ in range(...) instead. The reason is that repeat is faster and thus incurs less overhead per loop. Because I'm only interested in the addition/subtraction time it's actually preferable not to have the looping overhead messing with the timings (at least not that much).

🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ma.sum.html
numpy.ma.sum — NumPy v2.6.dev0 Manual
Return the sum of the array elements over the given axis. Masked elements are set to 0 internally. Refer to numpy.sum for full documentation.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndarray.sum.html
numpy.ndarray.sum — NumPy v2.0 Manual
Return the sum of the array elements over the given axis. Refer to numpy.sum for full documentation.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.sum.html
numpy.ndarray.sum — NumPy v2.5 Manual
ndarray.sum(axis=None, dtype=None, out=None, *, keepdims=<no value>, initial=<no value>, where=<no value>)#
🌐
Programiz
programiz.com › python-programming › numpy › methods › sum
NumPy sum() (With Examples)
The sum() function is used to calculate the sum of array elements along a specified axis or across all axes. import numpy as np array1 = np.array([1, 2, 3, 4, 5])
🌐
Codecademy
codecademy.com › docs › python:numpy › ndarray › .sum()
Python:NumPy | ndarray | .sum() | Codecademy
October 31, 2025 - The .sum() method returns the sum of array elements over a given axis.
🌐
DigitalOcean
digitalocean.com › community › tutorials › numpy-sum-in-python
numpy.sum() in Python | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › sum()
Python Numpy sum() - Calculate Array Sum
January 1, 2025 - This snippet calculates the sum of all elements in the data array, which results in 10. This is a straightforward example where sum() processes each element in a one-dimensional array. Consider a multi-dimensional array, such as a 2x3 matrix. Define the array and then apply numpy.sum() without specifying any axis.