Timeit: Make CLI function available from python

I believe rules about pinging are in the code of conduct. Ping them to bring them into the discussion or to reply if you’re not quoting, but not to remind them or if they said they wouldn’t comment further on the matter.

Also, could you fix the formatting of the example? Thanks.

1 Like

I like the separation of concerns, makes the code easier to grasp. The current main function is quite massive, parsing the input and running it all.


Yoav: before reading your post, I caught up to it and realized:

  1. What people really want is to be able to interactively get the entire CLI result, not just the warning. Part of the rationale to me is that two of the arguments can be multi-line source code, and these are much easier to work with in a proper IDE such as IDLE, where one has variables and statement editing. This is where I use timeit.
  2. One can currently do this by calling timeit.main with a simulated sys.argv[1:] list of strings.
timeit.main(['-uusec', '-vv', 'for i in range(1000): i*i'])
1 loop -> 4.03e-05 secs
2 loops -> 7.98e-05 secs
5 loops -> 0.0001918 secs
10 loops -> 0.0003822 secs
20 loops -> 0.0007609 secs
50 loops -> 0.001899 secs
100 loops -> 0.003813 secs
200 loops -> 0.00758 secs
500 loops -> 0.02006 secs
1000 loops -> 0.03859 secs
2000 loops -> 0.08046 secs
5000 loops -> 0.1914 secs
10000 loops -> 0.4017 secs

raw times: 4.024e+05 usec, 4.012e+05 usec, 3.97e+05 usec, 4.013e+05 usec, 4.017e+05 usec

10000 loops, best of 5: 39.7 usec per loop

But this is awkward.
3. timeit.main can pretty cleanly be split into sections that first parse the list of strings into the values needed and then use those values; the latter could then be moved into a callable function.

Your draft is close to what I imagined, except that I think the CLI behavior should remain as is and the run behavior the equivalent but with input adjustments. So I think everything from t = Timer... to the end should be moved into the new function and replaced with return run(...). verbose should be kept as an int, with a revised precision calculation. The unit error checking has to be moved. Make parameters after setup, and maybe that too, keyword only.

I think there is enough support here for a tracker issue: “Make CLI function available from python”, with summary of thread and explanation that aimed at interactive use. Then submit revised PR after making sure it passes test_timeit.


Thank you, I really appreciate it! This is my use case for timeit as well. Couldn’t have summarized it better.

I will start work on the tracker issue and improvements to the pull request soon.

Can’t wait to start working on it :slight_smile:

Issue: Make timeit CLI function available from python · Issue #119164 · python/cpython · GitHub
Will provide an update later in a separate reply.

FWIW: I have stopped using timeit in favor of pyperf, which provides much better tooling to measure runtime performance, similar to the old pybench I had written more than a decade ago.

1 Like