How can I time a code segment for testing performance with Pythons timeit?
Question: I've a python script which works just as it should but I need to write the
time for the execution. I've googled that I should use
[`timeit`](https://docs.python.org/2/library/timeit.html) but I can't seem to
get it to work.

My Python script looks like this:

    
    
    import sys
    import getopt
    import timeit
    import random
    import os
    import re
    import ibm_db
    import time
    from string import maketrans
    myfile = open("results_update.txt", "a")
    
    for r in range(100):
        rannumber = random.randint(0, 100)
    
        update = "update TABLE set val = %i where MyCount >= '2010' and MyCount < '2012' and number = '250'" % rannumber
        #print rannumber
    
        conn = ibm_db.pconnect("dsn=myDB","usrname","secretPWD")
    
    for r in range(5):
        print "Run %s\n" % r        
        ibm_db.execute(query_stmt)
     query_stmt = ibm_db.prepare(conn, update)
    
    myfile.close()
    ibm_db.close(conn)
    

What I need it the time it takes the execution of the query and written to the
file "results_update.txt". The purpose is to test an update statement for my
database with different indexes and tuning mechanisms.


Answer: you can use time.time ot time.clock before and after the block you want to
time

    
    
    import time
    
    t0 = time.time()
    code_block
    t1 = time.time()
    
    total = t1-t0
    

this method is not as exact as timeit (it does not average several runs) but
it is straighforward.

time.time() (in windows and linux) and time.clock(in linux) have not enough
resolution 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 time
    
    def myfast():
       code
    
    n = 10000
    t0 = 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 must be prefered to time.time().

