Skip to content

gh-137944: use argparse instead of getopt in timeit #137955

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

Closed
wants to merge 5 commits into from

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Aug 19, 2025

At some point, I wanted to reduce the cyclotomic complexity of main(), but I ended up with more lines than before, which defeated the purpose. While the function is a bit long, it's not that hard to follow, so I would rather keep a smaller diff.

@@ -362,7 +365,6 @@ def format_time(dt):
% (number, 's' if number != 1 else '',
repeat, format_time(best)))

best = min(timings)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR, best is already computed 5 lines above.

Comment on lines -347 to -348
scales = [(scale, unit) for unit, scale in units.items()]
scales.sort(reverse=True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR, this is constant for each invokation, so I can move it outside the function definition.

@picnixz
Copy link
Member Author

picnixz commented Aug 19, 2025

Ok, there is one possible breaking change:

$ python3.12 -m timeit '1+1' -n1 -r1
Traceback (most recent call last):
...
NameError: name 'n1' is not defined
$ ./python -m timeit '1+1' -n1 -r1
1 loop, best of 1: 410 nsec per loop

I need to find a way for argparse to gobble everything normally. I don't know if it's possible though.

@picnixz picnixz marked this pull request as draft August 19, 2025 13:11
@picnixz
Copy link
Member Author

picnixz commented Aug 19, 2025

Answer: use argparse.REMAINDER

@picnixz picnixz marked this pull request as ready for review August 19, 2025 13:23
@picnixz picnixz marked this pull request as draft August 20, 2025 15:09
Copy link
Member Author

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so there are a few bits that I could consider separate issues. For instance, if timeit.main() fails at the timer construction, then sys is not restored correctly.

On another note, scales could be extracted outside format_time function but that's mostly some cleanup code which doesn't really warrant a standalone PR.

So I can for now hold this PR because it's not really important. It's indeed a bit fragile and I'm worried about a possible unforseen change of behavior.

@picnixz picnixz closed this Aug 20, 2025
@picnixz picnixz deleted the refactor/timeit/argparse-137944 branch August 20, 2025 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants