How can I time a method of a class in python using Timeit

7stud bbxx789_05ss at yahoo.com
Thu May 24 14:33:55 EDT 2007


On May 24, 12:23 pm, "silverburgh.me... at gmail.com"
<silverburgh.me... at gmail.com> wrote:
> On May 24, 12:41 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
>
>
>
> > Actually, you can do this:
>
> > class Dog(object):
> >     def aFunction(self):
> >         result = 20 + 2
> >     def run(self):
> >         #do stuff
> >         aFunction()
> >         #do other stuff
> > import timeit
>
> > t = timeit.Timer("d.aFunction()", "from __main__ import Dog; d =
> > Dog()")
> > print t.timeit()
>
> > Since you only want to time aFunction(), you can call it directly.
>
> Can 't = timeit.Timer()' run inside a thread?
> And I have multiple threads running this 't = timeit.Time()' function?

__main__ is a thread isn't it?  However, timeit() runs in it's own
scope, so you need to import anything you need into that scope using
the setup parameter for the Timer() constructor.




More information about the Python-list mailing list