Using Python “timeit” to time functions
Posted: September 12, 2008 Filed under: modules, performance, programming, Python 2 Comments »Python’s timeit module is not intuitive for me.
Steve D’Aprano’s thread on the python mailing list: Timing a function object… and especially Carl Bank’s response of
def foo(x):
return x+1
import timeit
timeit.Timer("foo(1)","from __main__ import foo")
was a godsend!
Reading this hint contradicted my blithe statement to friend this morning that “the only time __main__ appears in user code is to determine when a script is being run form the command line”. Such ignorance and folly!
I ran into a similar situation this morning. However, it looks like your first and second links to the Python mailing list are broken.
Titus, sorry for the slow reply! Links are updated.