[Patches] [ python-Patches-1533909 ] Let timeit accept functions

SourceForge.net noreply at sourceforge.net
Thu Aug 3 16:32:38 CEST 2006


Patches item #1533909, was opened at 2006-08-03 10:32
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1533909&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Erik Demaine (edemaine)
Assigned to: Nobody/Anonymous (nobody)
Summary: Let timeit accept functions

Initial Comment:
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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1533909&group_id=5470


More information about the Patches mailing list