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

7stud bbxx789_05ss at yahoo.com
Thu May 24 13:41:16 EDT 2007


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.




More information about the Python-list mailing list