In 2.6, you can "just do it", per the docs:
Changed in version 2.6: The
stmt
andsetup
parameters can now also take objects that are callable without arguments. This will embed calls to them in a timer function that will then be executed bytimeit()
. Note that the timing overhead is a little larger in this case because of the extra function calls.
Are you stuck using an old version of Python? In that case, I would suggest taking Python 2.6's timer.py
sources and "backporting" them to the version you're stuck with -- should not be difficult if that version is 2.5 (gets harder the farther you need to travel back in time, of course). I would generally not recommend this for "production" code, but it's quite fine for code that supports testing, debugging, performance measurements, and the like.