-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Removed unnecessary call to reversed in for loop #125690
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
Conversation
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
I am not able to add "skip issue" & "skip news", can the maintainers add it? or maybe I can add, but not able to find it? |
It original code indeed used two generators, and this PR only one. But is this PR more readable (I think not) or faster (benchmark)? |
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
@eendebakpt Thank you for the review. Fixed it! For the comment on readability, I really believe, that people (who read this code) would know the range iterator and how it is used with start/end/step params. On the benchmark: I am not sure on how to benchmark this particular small thing, as there can be too many things that would interfere with benchmarking code which would try to benchmark this particular difference (at least, I am not so sure of how to perf test it). But, looking at proposed code, it is clear that an additional function call (stack frame allocation) is saved. |
You can do a quick benchmark (using ipython) with
That does not tell everything, but is a first indication of whether the code is faster or not. You do save a function call, but in the pr the arguments to range are more complex, so the end result could be faster, slower or ... about the same in performance. |
|
There was a detailed discussion in #108598 on improving the performance of @pochmann Is there a reason why for the proposal3b from #108598 (comment) no PR was created? |
I suspect this is unlikely to be worth it, absent incredible microbenchmarks. A |
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
The code uses Note the There aren't two levels of stack during iteration. The Thanks for the suggestion, but I will decline. |
@rhettinger Thanks for the feedback, makes sense! |
In random module's shuffle method while iterating over the list in reversed fashion, the code was using 2 generators (reversed, range). But as we know, we can simply use range generator alone (with proper start, end and step) to get the desired reversed iteration.
This is my first PR into my favourite programming language. Your feedback/suggestions are welcome and appreciated!
Note: As this is a minor change, have not opened an issue