You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sum() several times slower on Python 3 64-bit #68264
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-09-21.11:22:53.873>created_at=<Date2015-04-29.18:36:58.330>labels= ['interpreter-core', '3.11', 'performance']
title='sum() several times slower on Python 3 64-bit'updated_at=<Date2021-09-22.10:30:16.952>user='https://github.com/ambv'
I can't tell from initial poking what's the core issue here. Both examples produce equivalent bytecode, the builtin_sum() function is only noticably different in the fact that it uses PyLong_* across the board, including PyLong_AsLongAndOverlow. We'll need to profile this, which I didn't have time for yet.
Serhiy, this is 64-bit specific. Antoine, as far as I can tell, the main use case is: "Don't make it look like migrating to Python 3 is a terrible performance downgrade."
As we discussed on the language summit this year [1], we have to be at least not worse to look appealing. This might be a flawed benchmark but people will make them anyway. In this particular case, there's internal usage at Twitter that unearthed it. The example is just a simplified repro.
Some perf degradations were expected, like switching text to Unicode. In this case, the end result computed by both 2.7 and 3.4 is the same so we should be able to address this.
Łukasz: there are three ingredients here - sum, (x)range and the integer addition that sum will be performing at each iteration. Is there any chance you can separate the effects on your machine?
On my machine (OS X, 64-bit), I'm seeing *some* speed difference in the integer arithmetic, but not enough to explain the whole of the timing mismatch.
One thing we've lost in Python 3 is the fast path for small-int addition *inside* the ceval loop. It may be possible to restore something there.
Throwing out sum, I'm seeing significant slowdown simply from xrange versus range:
taniyama:Desktop mdickinson$ python2 -m timeit -s 'x = xrange(3, 10**9, 3)' 'for e in x: pass'
10 loops, best of 3: 5.01 sec per loop
taniyama:Desktop mdickinson$ python3 -m timeit -s 'x = range(3, 10**9, 3)' 'for e in x: pass'
10 loops, best of 3: 8.62 sec per loop
there are three ingredients here - sum, (x)range and the integer addition that sum will be performing at each iteration.
... not to forget the interpreter startup time on his machine. :)
I did a tiny bit of profiling and about 90% of the time seems to be spent creating and deallocating throw-away PyLong objects. My guess is that it simply lacks a free-list in _PyLong_New().
It seems we (like the benchmarks posted) are spending a whole lot of time on something that's probably not relevant to any real-world situation.
If someone has actual code that suffers from this, it would be good to know about it.
(note by the way that summing on a range() can be done O(1): it's just a variation on a arithmetic series)
I don't think it's irrelevant. Throw-away integers are really not uncommon. For-loops use them quite often, non-trivial arithmetic expressions can create a lot of intermediate temporaries. Speeding up the create-delete cycle of PyLong sounds like a very obvious thing to do.
Imagine some code that iterates over a list of integers, applies some calculation to them, and then stores them in a new list, maybe even using a list comprehension or so. If you could speed up the intermediate calculation by avoiding overhead in creating temporary PyLong objects, such code could benefit a lot.
I suspect that adding a free-list for single-digit PyLong objects (the most common case) would provide some visible benefit.
I don't think it's irrelevant. Throw-away integers are really not
uncommon. For-loops use them quite often, non-trivial arithmetic
expressions can create a lot of intermediate temporaries. Speeding up
the create-delete cycle of PyLong sounds like a very obvious thing to do.
That may be a good thing indeed. I'm just saying that the benchmarks
people are worried about here are completely pointless.
I tried implementing a freelist. Patch attached, mostly adapted from the one in dictobject.c, but certainly needs a bit of cleanup.
The results are not bad, about 10-20% faster:
Original:
$ ./python -m timeit 'sum(range(1, 100000))'
1000 loops, best of 3: 1.86 msec per loop
$ ./python -m timeit -s 'l = list(range(1000, 10000))''[(i*2+5) // 7 for i in l]'
1000 loops, best of 3: 1.05 msec per loop
With freelist:
$ ./python -m timeit 'sum(range(1, 100000))'
1000 loops, best of 3: 1.52 msec per loop
$ ./python -m timeit -s 'l = list(range(1000, 10000))''[(i*2+5) // 7 for i in l]'
1000 loops, best of 3: 931 usec per loop
If someone has actual code that suffers from this, it would be good to know about it.
You might have missed Łukasz' earlier comment: "In this particular case, there's internal usage at Twitter that unearthed it. The example is just a simplified repro."
Honestly, considering 2.7 has been EOL for a long time, I don't think we need to keep this specific issue open. I don't think we can do anything now and I would indeed prefer if faster-cpython comes up with a solution.
Now, for performance loss, we sometimes treat them as bug, sometimes not. I categorized it as a FR because we won't backport the change I think.
Honestly, considering 2.7 has been EOL for a long time, I don't think we need to keep this specific issue open.
Why not? If v2.7 is better somewhere - it's still a regression.
I don't think we can do anything now and I would indeed prefer if faster-cpython comes up with a solution.
I'm not sure that closing issue is a right thing even if we can't do anything now. Work is ongoing, e.g.: https://discuss.python.org/t/87950. I think it's a good thing to keep eye on this issue for people involved.
BTW, Py3.14 has impressive speedup, but I doubt it's related to integer arithmetic.
AFACIT, what changed between 3.13 and main (not 3.14, I haven't built 3.14 locally) is the time to iterate over:
$ python3.13 -m timeit -n1 -r1 -s 'r1 = range(3,10**9,3)' 'for _ in r1: pass'
1 loop, best of 1: 2.2 sec per loop
$ python3.15 -m timeit -n1 -r1 -s 'r1 = range(3,10**9,3)' 'for _ in r1: pass'
1 loop, best of 1: 1.57 sec per loop
We're roughly 30% faster between 3.13 and 3.15 just for iterations, which is roughly the gain between 3.13 and 3.14 for the benchmark above. I don't have a 2.7 that I can test but one possibility is that it's not just sum() that is impacted.
Activity
ambv commentedon Apr 29, 2015
I got a report that summing numbers is noticably slower on Python 3. This is easily reproducible:
real 0m6.165s
user 0m6.100s
sys 0m0.032s
real 0m16.413s
user 0m16.086s
sys 0m0.089s
I can't tell from initial poking what's the core issue here. Both examples produce equivalent bytecode, the builtin_sum() function is only noticably different in the fact that it uses PyLong_* across the board, including PyLong_AsLongAndOverlow. We'll need to profile this, which I didn't have time for yet.
serhiy-storchaka commentedon Apr 29, 2015
Can't reproduce on 32-bit Linux.
real 1m11.614s
user 1m11.376s
sys 0m0.056s
$ time python3.4 -c "print(sum(range(3, 10**9, 3)) + sum(range(5, 10**9, 5)) - sum(range(15, 10**9, 15)))"
233333333166666668
real 1m11.658s
user 1m10.980s
sys 0m0.572s
What is sys.int_info on your build?
pitrou commentedon Apr 29, 2015
I reproduce under 64-bit Linux. So this may be because the Python long digit (30 bits) is smaller than the C long (64 bits).
Lukasz: is there a specific use case? Note you can use Numpy for such calculations.
ambv commentedon Apr 29, 2015
Serhiy, this is 64-bit specific. Antoine, as far as I can tell, the main use case is: "Don't make it look like migrating to Python 3 is a terrible performance downgrade."
As we discussed on the language summit this year [1], we have to be at least not worse to look appealing. This might be a flawed benchmark but people will make them anyway. In this particular case, there's internal usage at Twitter that unearthed it. The example is just a simplified repro.
Some perf degradations were expected, like switching text to Unicode. In this case, the end result computed by both 2.7 and 3.4 is the same so we should be able to address this.
[1] http://lwn.net/Articles/640224/
pitrou commentedon Apr 29, 2015
If that's due to the different representation of Python 2's int type and Python 3's int type then I don't see an easy solution to this.
mdickinson commentedon Apr 30, 2015
Łukasz: there are three ingredients here - sum, (x)range and the integer addition that sum will be performing at each iteration. Is there any chance you can separate the effects on your machine?
On my machine (OS X, 64-bit), I'm seeing *some* speed difference in the integer arithmetic, but not enough to explain the whole of the timing mismatch.
One thing we've lost in Python 3 is the fast path for small-int addition *inside* the ceval loop. It may be possible to restore something there.
mdickinson commentedon Apr 30, 2015
Throwing out sum, I'm seeing significant slowdown simply from xrange versus range:
taniyama:Desktop mdickinson$ python2 -m timeit -s 'x = xrange(3, 10**9, 3)' 'for e in x: pass'
10 loops, best of 3: 5.01 sec per loop
taniyama:Desktop mdickinson$ python3 -m timeit -s 'x = range(3, 10**9, 3)' 'for e in x: pass'
10 loops, best of 3: 8.62 sec per loop
scoder commentedon Apr 30, 2015
... not to forget the interpreter startup time on his machine. :)
I did a tiny bit of profiling and about 90% of the time seems to be spent creating and deallocating throw-away PyLong objects. My guess is that it simply lacks a free-list in _PyLong_New().
pitrou commentedon Apr 30, 2015
It seems we (like the benchmarks posted) are spending a whole lot of time on something that's probably not relevant to any real-world situation.
If someone has actual code that suffers from this, it would be good to know about it.
(note by the way that summing on a range() can be done O(1): it's just a variation on a arithmetic series)
scoder commentedon May 1, 2015
I don't think it's irrelevant. Throw-away integers are really not uncommon. For-loops use them quite often, non-trivial arithmetic expressions can create a lot of intermediate temporaries. Speeding up the create-delete cycle of PyLong sounds like a very obvious thing to do.
Imagine some code that iterates over a list of integers, applies some calculation to them, and then stores them in a new list, maybe even using a list comprehension or so. If you could speed up the intermediate calculation by avoiding overhead in creating temporary PyLong objects, such code could benefit a lot.
I suspect that adding a free-list for single-digit PyLong objects (the most common case) would provide some visible benefit.
pitrou commentedon May 1, 2015
Le 01/05/2015 08:09, Stefan Behnel a écrit :
That may be a good thing indeed. I'm just saying that the benchmarks
people are worried about here are completely pointless.
scoder commentedon May 1, 2015
I tried implementing a freelist. Patch attached, mostly adapted from the one in dictobject.c, but certainly needs a bit of cleanup.
The results are not bad, about 10-20% faster:
Original:
With freelist:
stevendaprano commentedon May 1, 2015
Antoine asked:
You might have missed Łukasz' earlier comment: "In this particular case, there's internal usage at Twitter that unearthed it. The example is just a simplified repro."
31 remaining items
l1t1 commentedon Oct 26, 2022
3.11
vs
3.10
l1t1 commentedon Oct 26, 2022
pypy 7.3.9
StanFromIreland commentedon Apr 24, 2025
This can be closed. On 3.14:
Much faster than @ambv benchmarks.
Discussion of further optimization belongs elsewhere (faster-cpython).
skirpichev commentedon Apr 25, 2025
I'm not sure. Absolute numbers in benchmarks aren't relevant here. OP probably run py2 tests on a different system than you.
At least, you should run test for py2 and py3 on same system. Here are my tests.
Py2:
Py3.13:
Py3.14a7:
For me it looks like issue is valid and it's a regression from py2, not a feature request. @picnixz ? @ambv ?
picnixz commentedon Apr 25, 2025
Honestly, considering 2.7 has been EOL for a long time, I don't think we need to keep this specific issue open. I don't think we can do anything now and I would indeed prefer if faster-cpython comes up with a solution.
Now, for performance loss, we sometimes treat them as bug, sometimes not. I categorized it as a FR because we won't backport the change I think.
skirpichev commentedon Apr 25, 2025
Why not? If v2.7 is better somewhere - it's still a regression.
I'm not sure that closing issue is a right thing even if we can't do anything now. Work is ongoing, e.g.: https://discuss.python.org/t/87950. I think it's a good thing to keep eye on this issue for people involved.
BTW, Py3.14 has impressive speedup, but I doubt it's related to integer arithmetic.
gvanrossum commentedon Apr 25, 2025
I'm with @skirpichev. Can someone at least summarize an explanation of the difference between 2.7 and 3.x?
picnixz commentedon Apr 25, 2025
I think it was analyzed by #68264 (comment) but AFAICT, it's the call to
PyLong_AsLongAndOverflowthat introduced the regression.I wasn't aware of this one so thanks. By the way, feel free to re-open issues if I close them wrongly!
skirpichev commentedon Apr 25, 2025
My 2c:
Here more tests from my zoo:
picnixz commentedon Jun 29, 2025
AFACIT, what changed between 3.13 and main (not 3.14, I haven't built 3.14 locally) is the time to iterate over:
We're roughly 30% faster between 3.13 and 3.15 just for iterations, which is roughly the gain between 3.13 and 3.14 for the benchmark above. I don't have a 2.7 that I can test but one possibility is that it's not just sum() that is impacted.