[Tutor] str.split and quotes

Kent Johnson kent37 at tds.net
Fri Apr 8 12:00:03 CEST 2005

Marilyn Davis wrote:
> Is there a reason to prefer one over the other?  Is one faster?  I
> compiled my regular expression to make it quicker.

The only way to know which is faster is to time them both. The timeit module makes it pretty easy to 
do this.

Here is a simple example of using timeit for a different problem; you can adapt it to your own needs:

d = dict( ((i,i,i), i) for i in range(1000))

def withItems(d):
     for k,v in d.iteritems():

def withKeys(d):
     for k in d:

from timeit import Timer

for fn in [withItems, withKeys]:
     name = fn.__name__
     timer = Timer('%s(d)' % name, 'from __main__ import d, %s' % name)
     print name, timer.timeit(1000)


More information about the Tutor mailing list