I bet many of you have wondered, at least once, how much time your method or class is going to take when executed. Well, thanks to the python timeit module we can really time our own code.
Quite often when tweaking with the code, it would be quite a good practice to test such tweaks by means of timing them; in this way we could be able to see which tweaked version of the code takes the more time. In this tutorial we’re going into the timeit module which has been added since python 2.3.
Using the aforementioned module is definitely simple; indeed with just three lines of code we shall be able to assess the execution time of our script, or part of it that is.
Let’s create a file called “test_timeit.py” in our working directory and let’s also copy the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import timeit class Tester: def __init__(self): self.MAX = 100 self.my_list = [] def add_to_list(self): for i in range(self.MAX): self.my_list.append(i) def remove_from_list(self): for elem in self.my_list: self.my_list.remove(elem) if __name__=="__main__": inst = Tester() t1 = timeit.Timer("inst.add_to_list()", "from __main__ import inst") #t1 = timeit.Timer("inst.remove_from_list()", "from __main__ import inst") try: print t1.timeit(200) print t1.repeat(repeat=3, number=200) except: t1.print_exc() |
Now for the explanatory part:
First of all we created a simple class named “Tester”, thus bundling two methods which we shall use to determine the execution time; they’re called add_to_list() and remove_to_list() and they’ll take care of adding or removing elements from our list, named as “my_list”.
Let’s get to the __main__ part now where, thanks to inst = Tester(), we shall create a new instance of our Tester class—this instance will let us reach each method which has to be timed.
Then from line 21 onwards, we tackle the timeit part.
The first one:
t1 = timeit.Timer("inst.add_to_list()", "from __main__ import inst")
by which we create an instance of the Timer class that will let us measure the execution time of our python code—each parameter used in this example is referring respectively to the method we time, namely “inst.add_to_list()”, and to the mandatory import which is enabling us to get an instance called inst.
NOTE: We would get the same result with: t1 = timeit.Timer(”Tester().add_to_list()”, “from __main__ import Tester”)
Now, getting to the real time-appraising part, the Timer class deploys two methods:
timeit([number=1000000])
This method returns the execution time of the main statement, which has been recalled as many times as parameter “number” dictates; in our example(line 25) we’ve used print t1.timeit(200), that is to say, the add_to_list() method of Tester class will be called for 200 times, at the end of which the amount of time(float) spent on such office will be returned.
repeat([repeat=3[, number=1000000]])
The repeat() method works in the same way of timeit(), just with a trifling variation; quite useful.
The number specified by “repeat” pinpoints how many times we take the measure.
As it is in our example, print t1.repeat(repeat=3, number=200), the method add_to_list() of Tester class is being recalled 200 times, at the end of which a new measure ensues, and so on as many times as it is indicated in the number “repeat”. As we can easily guess, the repeat() method returns a list with every recorded amount of time.
Here come other two simple and explanatory examples:
#Example 1 def factorial(x): if x == 0: return 1 else: return x * factorial(x-1) if __name__=="__main__": from timeit import Timer t = Timer("factorial(5)", "from __main__ import factorial") print t.timeit() #Example 2 import timeit s = """ for i in range(10): if i % 2 == 0: pass """ t= timeit.Timer(s) print t.timeit()
As you can clearly see, using timeit is that easy, with ever so few steps we were able to time our python code.
Well? What are you waiting for? Get on with your testing!
created by Damiano Porta and translated by Daniele Feuli