Can't decipher how to use `timeit`

The ipython magic gives following detailed output

In [1]: %timeit pass
8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

From lib/timeit, the module’s CLI gives this output; a bit less detailed

$ python3 -m timeit '"-".join(str(n) for n in range(100))'
10000 loops, best of 5: 30.2 usec per loop

The Python interface gives this super trimmed down output.

>>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
0.3018611848820001

Is there any way to get the detailed output via the python interface itself?
As i was totally uncapable of making the magic way to work properly in my case.

(will post a detailed input on that in the comments below a bit later, so, reserving those for now)

reserved comment 1

reserved comment 2

If you want to know what the command line version does you can read the code.

$ python3
Python 3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit
<module 'timeit' from '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/timeit.py'>
>>> ^D

From the repr of timeit you can see where the code is and pull that up in your editor.

The main() function has all the logic to print the results and from that you can work out how to do what you need.

1 Like

Hmm, right. I never called the main function before and it’s not documented, but it works:

import timeit

timeit.main(['"-".join(str(n) for n in range(100))'])

Sample output (Attempt This Online!):

10000 loops, best of 5: 21.4 usec per loop

Another way to see the source code is btw to click on the “Source code” link at the top of the module’s documentation.

1 Like

Hmm, sounds like some part of main() should be extracted out for a more readable output. Perhaps the format_time() bit?

As it happens, I’ve done some work on trying to improve the interface of timeit. I have a (subdirectory of a) repository here:

and wrote about it here:

Of course, many of the changes I made are things the standard library timeit module could not hold itself free to change - for example, the standard library cannot reasonably rely on third-party libraries (for testing) :slight_smile: I also haven’t attempted to make peptides an installable package or anything, since it’s more just a place to collect scraps of “if I had my druthers” code. However, you might find something useful there.

@whatTheNameIs
I’m not sure if what I have to post here is of any help to you, but (least ways) it does speak to the title of your post: “Can’t decipher how to use ‘timeit’”

After posting some code to help @saradindu, in which @steven.rumbalski also posted a valid solution, I remembered your post and thought that maybe I could use the two examples to illustrate how to use ‘timeit’, so here goes:

import timeit

re_test = '''\
import re

file = """00:00:08
ss ssone 32.0
ssone sstwo 33.0
sstwo ssthree 35.0
11:12:13
ssfour ssfive 34.0
ssthree sssix 38.0
ssnine ssseven 39.0
13:12:11
"""

result = re.findall(r'(\d+(?:\.\d+){0,1})', file)
'''

loop_test = '''\

file = """00:00:08
ss ssone 32.0
ssone sstwo 33.0
sstwo ssthree 35.0
11:12:13
ssfour ssfive 34.0
ssthree sssix 38.0
ssnine ssseven 39.0
13:12:11
"""

file_list = file.split()

output = []

for item in file_list:
    if item[0].isdigit():
        if ':' in item:
            output += [n for n in item.split(':')]
        else:
            output.append(item)
'''

print(timeit.Timer(stmt=re_test).timeit(number=1000000))
print(timeit.Timer(stmt=loop_test).timeit(number=1000000))

The output I get from that is:

36.97096371900989
23.56015043699881

I’m as sure as I can be that I’ve used timeit correctly, but if I haven’t, I’m also as sure as I can be that someone will correct me and my code.

The post from which the code has been taken can be found here