En Sun, 30 Mar 2008 00:20:34 -0300, Steven D'Aprano <[EMAIL PROTECTED]> escribió:
> On Sat, 29 Mar 2008 21:12:33 -0300, Gabriel Genellina wrote: > >> def main(): >> global func >> func = factory() >> return timeit.Timer('func()', 'from __main__ import func').timeit() > > Alas, this does not work, because all the Timer instances share the same > state. Second try: <code> # timeanyfunc.py from timeit import Timer from itertools import count _counter = count() def timeanyfunc(fn, *args, **kw): def wrapper(fn=fn, args=args, kw=kw): return fn(*args, **kw) wrappername = 'wrapper%d' % _counter.next() globals()[wrappername] = wrapper return Timer("%s()" % wrappername, "from %s import %s" % (__name__, wrappername)) </code> Horrible, I know. Those wrapper1,wrapper2,wrapper3... keep growing with each call. But it's the only way I could find, at least without changing the code template used by timeit. py> from timeanyfunc import timeanyfunc py> py> def functionA(arg): ... print "Function A",arg ... py> def functionB(arg): ... print "Function B",arg ... py> def test_timer(func1, func2): ... T1 = timeanyfunc(func1, "arg1") ... T2 = timeanyfunc(func2, "arg2") ... print "Calling %s" % func1.__name__ ... T1.repeat(3, 1) ... print "Calling %s" % func2.__name__ ... T2.repeat(3, 1) ... py> test_timer(functionA, functionB) Calling functionA Function A arg1 Function A arg1 Function A arg1 Calling functionB Function B arg2 Function B arg2 Function B arg2 -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list