Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Code Changes
I have found that initializing a namedtuple costs about twice the time as initializing a tuple. With respect to this module, I setup a list of 1M single-digit integers to profile the rolling objects in the
minmax
module against using theline_profiler
tool. I used a window size of 50 -- although the particulars of how many numbers and window sizing is inconsequential to the profiling results below.Briefly, here are the results of profiling the original
_update_window()
function for theMax
object:Of note is the
Per Hit
field. According to theline_profiler
documentation, this isThe timer's unit for this particular test is 1 microsecond.
Line 139 takes one microsecond to run due to creating the
pair
namedtuple object.Switching from namedtuples to tuples, here are the results after profiling:
Here, you can see line 143 running in half the time as before.
A final (very minor) change is to use
if
overwhile
when removing any maxima/minima that should exit the window. Making this change won't disrupt the end-result because at most one value enters the window in any iteration. Here are the results after profiling:The notable metric here is the reduced number of
Hits
on line 146 relative to the previous two profiling results.Bookkeeping
All tests pass.
I have also incremented the patch version and added versions 0.3.0 and 0.3.1 (this PR) to the changelog.