How can I time a method of a class in python using Timeit
7stud
bbxx789_05ss at yahoo.com
Thu May 24 13:38:54 EDT 2007
On May 24, 11:30 am, 7stud <bbxx789_0... at yahoo.com> wrote:
> On May 24, 9:36 am, "silverburgh.me... at gmail.com"
>
>
>
> <silverburgh.me... at gmail.com> wrote:
> > Hi,
>
> > I am using timeit to time a global function like this
>
> > t = timeit.Timer("timeTest()","from __main__ import timeTest")
> > result = t.timeit();
>
> > But how can i use timeit to time a function in a class?
> > class FetchUrlThread(threading.Thread):
> > def aFunction(self):
> > # do something ....
>
> > def run(self):
> > # how can I time how long does aFunction() take here?
> > aFunction();
>
> > Thank you.
>
> How about this:
>
> class Dog(object):
> def run(self):
> result = 10 * 20 + 3
>
> import timeit
>
> t = timeit.Timer("d.run()", "from __main__ import Dog; d = Dog()")
> print t.timeit()
Actually, you could 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()
It doesn't matter if you call aFunction() directly if all you want to
do is time aFunction().
More information about the Python-list
mailing list