Bug Question Why Is Python's sum() So Slow for Large Lists? Can It Be Optimized?

Currently reading:
 Bug Question Why Is Python's sum() So Slow for Large Lists? Can It Be Optimized?

Thread representing a bug.
Thread represents a question

trly.5

Member
LV
0
Joined
Jan 12, 2025
Threads
2
Likes
0
Credits
165©
Cash
0$
Hey everyone,

I’m working on a project where I need to process a lot of numerical data efficiently. One common task I have is summing up large lists of numbers (e.g., millions of floats). Naturally, I’ve been using Python’s built-in sum() function for this, but I’ve noticed it’s not as fast as I expected.
Example: https://pastebin.com/embed_js/bfzCs5Hp
For a list this large, sum() takes over a second on my machine. When I compare it to summing in other languages (like C or even NumPy’s np.sum()), the difference is huge!

I’ve done some research and found that sum() operates at the Python level, which adds overhead because of the interpreted nature of the language. But given how common summing is, I’m wondering
  • Why doesn’t Python’s sum() use optimized C code under the hood for large lists?
  • re there ways to speed up sum() without switching entirely to something like NumPy?
I tried experimenting with reduce() and even a custom C extension, but I’m not sure if I’m overthinking it. Here’s what I tried with reduce():

It’s slower than the built-in sum()! So now I’m stuck. Any help?
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Top Bottom