Assignment saves time?

rpglover64 at gmail.com rpglover64 at gmail.com
Fri Feb 15 15:46:59 EST 2008


I ran it in an interactive python shell, which might have made a
difference.

import timeit
time1=timeit.Timer('s=len(li); s==1000', 'li=range(1000)')
time2=timeit.Timer('len(li)==1000', 'li=range(1000)')
store=min(time1.repeat(5))
call=min(time2.repeat(5))
store=min(min(time1.repeat(5)),store)
call=min(min(time2.repeat(5)),call)
store
  0.25531911849975586
call
  0.25700902938842773

The difference is small enough to be insignificant, but I am curious
how it's possible and why it happened.  It's more likely a reflection
of how I timed it than anything else, isn't it?



More information about the Python-list mailing list