Passing function objects to timeit

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 30 09:03:11 EDT 2008


On Sun, 30 Mar 2008 10:55:25 +0000, Steven D'Aprano wrote:

> Perhaps it's time for me to take a different approach. Since I can't
> call timeit, and I can't inherit from it (same problem with state being
> shared between instances), perhaps I should write my own timer.
> 
> 
> import timeit
> import gc
> import itertools
> 
> def timeit2(func, args=[], kwargs={}, it=None,
>             timer=timeit.default_timer):
>     if it is None: it = itertools.repeat(None, timeit.default_number)
>     save_gc = gc.isenabled()
>     gc.disable()
>     try:
>         start = timer()
>         for counter in it:
>             func(*args, **kwargs)
>         end = timer()
>     finally:
>         if save_gc:
>             gc.enable()
>     return end - start



I've done some comparisons, and the times reported by this function are 
consistently almost double that of times reported by timeit.

Now, I don't expect that my code and the timeit code should have the same 
overhead, and I realise that the variability of timer results is large. 
But I'm quite surprised that this seems to have so much more overhead. 
Can anyone offer some advice?



-- 
Steven



More information about the Python-list mailing list