-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Make an O(1)
fastpath for sum(range(...))
#107868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
O(1)
fastpath for sum(range(...))
O(1)
fastpath for sum(range(...))
Hello, and thanks for your effort in writing issue/PR! I'm 50/50 on it. |
This seems like too rare a use-case to be worth the extra complexity in the |
Hopefully any programmer who actually has
If you know the exact last number |
I would also argue that sometimes statements like In general I do not think that it is desiderable to outsmart the programmer writing |
The similar |
I am negative on this change. Complexity over rare use case. |
It seems the overall consensus is that this change is not worth the added complexity and so I will close this issue and my PR. Thanks for the comments. Since I am just starting out contributing to cpython, for my own knowledge, would one of you mind quickly clarifying the two questions I asked in these comments in my PR:
>>> sum(range(2**61))
2658455991569831744654692615953842176
>>> sum(range(2**62))
10633823966279326980924613473029062656
>>> sum(range(2**63 - 1))
42535295865117307919086767873688862721
>>> sum(range(2**63))
Segmentation fault (core dumped) It can obviously generate numbers larger than the max integer size, and I think every operation in my implementation uses >>> sum(range(2**1000, 2**1000 + 1000, 999))
21430172143725346418968500981200036211228096234110672148875007767407021022498722449863967576313917162551893458351062936503742905713846280871969155149397149607869135549648461970842149210124742283755908364306092949967163882534797535118331087892154125829142392955373084335320859663305248773674411336139751 Thanks in advance. |
Closing per Marco's rationale. A |
Yes: each Lines 1769 to 1794 in cc2cf85
There are lots of potential causes for segfaults in Python C-API code; refcounting and exception-handling issues are probably the most common, but by no means the only ones. In this particular case, I think you're running into the issue that >>> len(range(2**63))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t So your call to
|
@mcognetta You might find the tutorial useful on exception handling and reference counting: |
IMHO, this kind of optimization should be achieved by bytecode-level specialization, not runtime-level optimization. |
If we had a multitude of compressed (vitural) range collection objects, then it might make sense to add sum and possibly even stdev special method objects. But we do not. (And to me this seems like something for PyPI.) |
@mdickinson Thanks for the detailed comment! That makes sense to me. One last note, I had planned for another range optimization: checking >>> import time
>>> r = range(100000000)
>>> t=time.time();8000000 in r;print(time.time()-t)
True
4.57763671875e-05
>>> t=time.time();8000000.0 in r;print(time.time()-t)
True
0.20510268211364746 I imagine this would similarly be rejected, but I am putting it here as another place where operations on range are quick to fall back on an iterative method. Anyway, thanks again for the comments, I appreciated the discussion. |
Take a look at https://discuss.python.org/t/fast-path-for-float-in-range-checks/18248 for previous discussion. Indeed there didn't seem to be much consensus that this would be a good idea. |
Currently,
sum(range(...))
falls back to an iterative loop that takesO(n)
time, wheren
is the length of the range. Instead, this can be done inO(1)
time with some arithmetic.This is slightly related to issue #68264, where it is noted that
sum(range(...))
had a slightly performance regression between Python2 and 3. However, in both cases, anO(n)
time implementation is used.Note: this is my first time contributing here using only Github. I already have a patch prepared, but I think I needed to open an issue first.
Linked PRs
O(1)
fastpath forsum(range(...))
#107870The text was updated successfully, but these errors were encountered: