Getting started with python

James Stroud jstroud at mbi.ucla.edu
Sun Apr 15 23:49:53 EDT 2007


Steve Holden wrote:
> You'd be worth more if you'd used elif and omitted the continue 
> statements, but for a first solution it's acceptable.

Depends on what you are after.

py> s = """
... for i in xrange(1,101):
...   if not i % 15:
...     continue
...   if not i % 5:
...     continue
...   if not i % 3:
...     continue
...   else:
...     pass
... """
py> t = timeit.Timer(stmt=s)
py> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
40.49 usec/pass
py> s = """
... for i in xrange(1,101):
...   if not i % 15:
...     pass
...   elif not i % 5:
...     pass
...   elif not i % 3:
...     pass
...   else:
...     pass
... """
py> t = timeit.Timer(stmt=s)
py> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
40.88 usec/pass

James



More information about the Python-list mailing list