Skip to content

Timeit same units - #15485

Open
mina375 wants to merge 4 commits into
ipython:mainfrom
mina375:timeit-same-units
Open

mina375 wants to merge 4 commits into
ipython:mainfrom
mina375:timeit-same-units

Conversation

@mina375

@mina375 mina375 commented Nov 19, 2026

Copy link
Copy Markdown

Summary

Modified the format of the time to be the same unit between the mean and deviation

Before:
1.28 ms ± 37 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

After:
1.28 ± 0.0134 ms per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

I checked that the new test fails without the change in execution.py.

Code changes

  • Separated the unit selection from _format_time() into a small helper function _time_unit(). The behavior of _format_time() is not changed.
  • TimeitResult.__str__() uses the same unit selected for the mean for both values.
  • If the mean is 60 seconds or more, the output format is the same as before (e.g. "1min 5s").
  • Added a test for TimeitResult output with UTF-8 and ASCII.

References

Refs #14489

AI usage

  • Tool: Claude (Anthropic)
  • Model: Opus 5.5
  • Effort level: Unknown (I got feedback on my code from AI)
  • Human supervision (did a person direct the work, read the whole diff and run the change?)

Checklist

  • I understand that if I submit a pull request similar to an earlier one, without strongly arguing for a different implementation, I risk a 30-day ban (see Bans).
  • I understand that if I open a pull request while more than 30 pull requests are open on the repository, without also helping to review other pull requests, I risk a temporary ban as well (see Bans).

@Carreau

Carreau commented Nov 19, 2026

Copy link
Copy Markdown
Member

Thanks, as for my first comment in the issue, I'm still unsure this is the right approach as when there are many order of magnitude this becomes unreadable with too many zeros in front, and other people have asked ofr other changes like not using stddev... etc.

So I'd like to find a better solution instead of just blindly trying to fix the linked issue,
potetially ship on PyPI another extension that changes how the timeit magic works, iterate on that with all the change you like and swap/recommend it here later.

@mina375

mina375 commented Nov 20, 2026

Copy link
Copy Markdown
Author

Thank you for the comment.
How about adding a condition that this is applied when the mean and deviation are within a certain number of digits (e.g. 3digits)?
If you think this is still not satisfying, I would consider another way (including the one you suggested).
In that case, feel free to close this PR.

@Carreau

Carreau commented Nov 20, 2026

Copy link
Copy Markdown
Member

hum, maybe that could work. I was also thinking we could also maybe use 10x(exponent multiple of 3) when relevant; which would keep the same unit but make it easier to read. In general even if this goes in or not: i still encourage you to try to build it as an extension.

in particular i believe this is often also use to compare multiple
run/versions, so i was thinking of have a --name STRING option. that would store the results (you can return them with -o already)

then a %timit-graphagic that would automatically collect all the recent result and compare them in say a box plot (or violin plot)

I never took the time to actually think and implement that; and would likely not do it in core ipython.

But as an extension that would be great; it could also fold into it a bunch of other type of measurement (memory; number of cores...) advantage of an extension is it can update and experiment faster than core and if stable enough we can make it a dependency one day.

@mina375

mina375 commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

Thank you for the reply.
I added the functionality you mentioned (displaying a power of ten that is a multiple of 3 when the number is small, e.g. 10 ± 1 ×10⁻⁶ ms per loop).
I am interested in developing the extension you suggested apart from this PR.
I would leave it to you whether this is merged or closed (as it will take time to develop the extension).

This branch has not been deployed

No deployments
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.

2 participants