Python: Time a code segment for testing performance (with timeit)

Posted by Mestika on Stack Overflow See other posts from Stack Overflow or by Mestika
Published on 2010-05-19T14:24:46Z Indexed on 2010/05/19 14:40 UTC
Read the original article Hit count: 174

Filed under:
|
|
|
|

Hi,

I've a python script which works just as it should but I need to write the time for the execution. I've gooled that I should use timeit 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.

Sincerely

Mestika

© Stack Overflow or respective owner

Related posts about python

Related posts about timeit