Getting started with python

Steve Holden steve at holdenweb.com
Mon Apr 16 07:44:15 EDT 2007


James Stroud wrote:
> 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
> 

To be strictly comparable you should have pass statements before the 
continue statements as well.  Ignoring that, clearly it's well worth 
saving that extra 390 nanoseconds each time round the loop.

Repeat after me "premature optimization is the root of all evil".

   http://en.wikipedia.org/wiki/Optimization_(computer_science)

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list