Hacker News
new
|
past
|
comments
|
ask
|
show
|
jobs
|
submit
login
kristianp
on Dec 22, 2022
|
parent
|
context
|
favorite
| on:
Avoid exception throwing in performance-sensitive ...
Is that still true in recent Python versions? Sounds like low-hanging fruit for perf optimisation.
tsimionescu
on Dec 22, 2022
|
next
[–]
Yes, the for loop still calls next() on the iterator until it throws a StopIteration exception.
sgtnoodle
on Dec 22, 2022
|
prev
|
next
[–]
Using exceptions in python isn't any more expensive than not using exceptions because the interpreter pays the same cost no matter what. It's a core design decision. Changing it would probably break some things.
stevesimmons
on Dec 22, 2022
|
prev
[–]
Python 3.11 made the "try" part of exceptions zero-cost. The "except" part only has overhead if the exception is triggered.
diarrhea
on Dec 22, 2022
|
parent
[–]
So the answer to the question is yes, because for loops signal their finishing by raising an exception.
Guidelines
|
FAQ
|
Lists
|
API
|
Security
|
Legal
|
Apply to YC
|
Contact
Search: