How can I time a code segment for testing performance with Pythons timeit? How can I time a code segment for testing performance with Pythons timeit? python python

How can I time a code segment for testing performance with Pythons timeit?


You can use time.time() or time.clock() before and after the block you want to time.

import timet0 = time.time()code_blockt1 = time.time()total = t1-t0

This method is not as exact as timeit (it does not average several runs) but it is straightforward.

time.time() (in Windows and Linux) and time.clock() (in Linux) are not precise enough for fast functions (you get total = 0). In this case or if you want to average the time elapsed by several runs, you have to manually call the function multiple times (As I think you already do in you example code and timeit does automatically when you set its number argument)

import timedef myfast():   coden = 10000t0 = time.time()for i in range(n): myfast()t1 = time.time()total_n = t1-t0

In Windows, as Corey stated in the comment, time.clock() has much higher precision (microsecond instead of second) and is preferred over time.time().


If you are profiling your code and can use IPython, it has the magic function %timeit.

%%timeit operates on cells.

In [2]: %timeit cos(3.14)10000000 loops, best of 3: 160 ns per loopIn [3]: %%timeit   ...: cos(3.14)   ...: x = 2 + 3   ...: 10000000 loops, best of 3: 196 ns per loop


Quite apart from the timing, this code you show is simply incorrect: you execute 100 connections (completely ignoring all but the last one), and then when you do the first execute call you pass it a local variable query_stmt which you only initialize after the execute call.

First, make your code correct, without worrying about timing yet: i.e. a function that makes or receives a connection and performs 100 or 500 or whatever number of updates on that connection, then closes the connection. Once you have your code working correctly is the correct point at which to think about using timeit on it!

Specifically, if the function you want to time is a parameter-less one called foobar you can use timeit.timeit (2.6 or later -- it's more complicated in 2.5 and before):

timeit.timeit('foobar()', number=1000)

You'd better specify the number of runs because the default, a million, may be high for your use case (leading to spending a lot of time in this code;-).