I ran the following in the python shell:
>>> import timeit
>>> storeit=timeit.Timer('s=len(li);s==1000', 'li=range(1000)')
>>> checkit=timeit.Timer('len(li)==1000', 'li=range(1000)')
>>> listofresults=[(min(storeit.repeat(5)),min(checkit.repeat(5))) for i in
>>> xrange(20)]
listofresults contain
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)
cal
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)
cal
In timing my code, which I probably screwed up, I found something
curious:
given that li=range(1000),
the code block (enclosed in braces for clarification)
{
size=len(li)
size==1000
}
runs slightly faster than
{
len(li)==1000
}
I tested this using the timeit module, and though the differenc