classification
Title: Let timeit accept functions
Type: Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: edemaine, georg.brandl
Priority: normal Keywords: patch

Created on 2006-08-03 14:32 by edemaine, last changed 2007-03-13 19:32 by georg.brandl. This issue is now closed.

Files
File name Uploaded Description Edit
patch edemaine, 2006-08-03 14:32 Patch implementing proposed changes to timeit
Messages (3)
msg50816 - (view) Author: Erik Demaine (edemaine) Date: 2006-08-03 14:32
I see that there is a history of proposed (and
rejected) patches to allow timeit to see various module
global namespaces, etc.  But I'm surprised that no one
has proposed the obvious functional solution: allow the
arguments (particularly 'stmt') to be functions that
get called, instead of strings that get parsed and
executed.  This does increase the measurement overhead
slightly, adding in the function call, but in many
cases it is far more useful within scripts.  To time
some part of the code, you can replace a function call
'foo()' with 'timeit.Timer(foo).timeit()'.

I also propose helper functions for use within scripts:
timeit.timeit(...) is shorthand for
timeit.Timer(...).timeit(...), and timeit.repeat(...)
is shorthand for timeit.Timer(...).repeat(...).  Now
you can replace a function call 'foo()' with
'timeit.timeit(foo)', e.g., 'print "foo takes",
timeit.timeit(foo), "seconds"'.

Attached is a simple patch implementing both of these
changes.  Documentation would need updating too.
msg50817 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-08-03 18:12
Logged In: YES 
user_id=849994

Please use four-space indents when contributing library
code. Perhaps unicode "stmt" arguments should also be
allowed. Perhaps other arguments should be checked with
callable() to exclude lists or something like that.
msg50818 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-13 19:32
Reformatted the patch, bugfixed it, added documentation and committed as rev. 54348.
History
Date User Action Args
2006-08-03 14:32:38edemainecreate