On Fri, 8 Apr 2005, Kent Johnson wrote:
> 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:
Thank you so much again.
This will be handy.
Marilyn
>
> d = dict( ((i,i,i), i) for i in range(1000))
>
> def withItems(d):
> for k,v in d.iteritems():
> pass
>
>
> def withKeys(d):
> for k in d:
> d[k]
>
>
> 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)
>
>
> Kent
>
> _______________________________________________
> Tutor maillist - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
--
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor