In fact, it is really very rare to use the test application run time to calculate the situation. For a long time did not do performance optimization work, whether it is cprofile or Timeit modules have been unfamiliar for a long time no use, I have mentioned in the previous Article Cpfile performance test use, but has not used this more lightweight run time Measurement library has been carefully practice summary, Let's summarize today.
Starting with the simplest examples, for example, we want to test whether a list derivation is much faster than the normal write for.
Import"" "sum = []for i in Range (£): sum.append (i)" ""print Timeit.timeit (stmt="[I for I in range]", number=100000)print Timeit.timeit (stmt=foooo, number=100000)
Output:
3.79257702827
9.0510661602
It is not difficult to see that the use of the list derivation is more than normal using the list to append elements through the 10w cycle will be nearly 5.3 seconds faster, nearly three times times faster.
The Timeit module is abstracted out; two methods that can be used directly, wrapping a layer can let us not care about the internal implementation, the following look at the code inside the module:
defTimeit (stmt="Pass", setup="Pass", timer=Default_timer, number=default_number):"""convenience function to create Timer object and call Timeit method.""" returnTimer (stmt, Setup, timer). Timeit (number)defRepeat (stmt="Pass", setup="Pass", timer=Default_timer, repeat=default_repeat, number=default_number):"""convenience function to create Timer object and call repeat method.""" returnTimer (stmt, Setup, timer). Repeat (Repeat, number)
You can see that both of these methods wrap a layer of these parameters on the timer class:
stmt: This parameter is statement, you can put the code to calculate the time in the inside. He can accept the expression of a string directly, or it can accept a single variable or accept a function.
Setup: This parameter can be used to transfer the stmt environment. such as a variety of import and parameters or something.
Timer: This parameter is generally not used, the specific use can refer to the document.
The timer class also has the repeat and the Timeit method to use is also very convenient is Timeit.timeit and timeit.repeat.
One is the above example of the Timeit, one is repeat actually repeat than timeit more than the number of times to execute a timer parameter. This number of executions returns the time of each execution as an array.
Import"" "sum = []for i in Range (£): sum.append (i)" ""print Timeit.repeat (stmt="[I for I in range]", repeat=2, number=100000)
Output
[4.466734170913696, 4.255025148391724]
We can do this by taking min, average, and maximum values for all execution times to get the data we want. The staff was very friendly and helpful.
A slightly advanced example is the example of the setup parameter that I used when testing protocol buffer python:
#Coding:utf-8ImportTimeit#Initialize classx ="""Say_hi. Parsefromstring (P)"""y="""simplejson.loads (x)"""PrintTimeit.timeit (Stmt=x, setup="import SAY_HI_PB2;" "Say_hi = say_hi_pb2. Sayhi ();" "say_hi.id = 13423;" "say_hi.something = ' Axiba ';" "say_hi.extra_info = ' Xiba ';" "P =say_hi. Serializetostring ()", number=1000000)PrintTimeit.timeit (Stmt=y, setup="import Simplejson;" "json={" "' id ': 13423," "' something ': ' Axiba '," "' extra_info ': ' Xiba '," "};" "x = simplejson.dumps (JSON)", number=1000000
How to use Python Timeit module usage practices