timeit module for comparing the performance of two scripts

Phoe6 orsenthil at gmail.com
Tue Jul 11 11:33:13 EDT 2006


Hi,
       Following are my files.
In the format:

Filename
----
content
----

config1.txt
----
#DataBase Repository file
dbRepository = omsa_disney.xml
----

config2.txt
----
# Configfile for sendmail

[Sendmail]
userprefix = testuser
----

pyConfig.py
----
import re

def pyConfig():
    fhandle = open( 'config1.txt', 'r' )
    arr = fhandle.readlines()
    fhandle.close()

    hash = {}

    for item in arr:
        txt = item.strip()
        if re.search( '^\s*$', txt ):
            continue
        if re.search( '^#.*$', txt ):
            continue
        if not re.search( '^\s*(\w|\W)+\s*=\s*(\w|\W)+\s*$', txt ):
            continue
        hash[txt.split( '=' )[0].strip().lower()] = txt.split( '='
)[1].strip()

    print hash['dbrepository']
----

pyConparse.py
----
from ConfigParser import ConfigParser

def pyConParse():
    configparser = ConfigParser()
    configparser.read('config2.txt')
    print configparser.get('Sendmail','userprefix')

----

Question is:
How do I compare the performance of pyConfig.py vs pyConparse.py using
timeit module?
I tried it as the way it is mentioned in the example, but when I do
Timer("pyConfig()","from __main__ import pyConfig"), it imports
pyConfig in infinite loop. ( How does the python doc example on timeit
work? Anyone tried it?)

I just need to compare pyConfig.py and pyParse.py. ( And as general
question x.py and y.py)
How I can do it? Please consider this as a newbie question as well.

Thanks,
Senthil




More information about the Python-list mailing list