Assignment saves time?

cokofreedom at gmail.com cokofreedom at gmail.com
Fri Feb 15 07:27:33 EST 2008


> $ python -m timeit -s 'l=[]' 'len(l)==1000'
> 1000000 loops, best of 3: 0.256 usec per loop
> $ python -m timeit -s 'l=[]' 'len(l)==1000'
> 1000000 loops, best of 3: 0.27 usec per loop
>
> $ python -m timeit -s 'l=[]' 's=len(l); s==1000'
> 1000000 loops, best of 3: 0.287 usec per loop
> $ python -m timeit -s 'l=[]' 's=len(l); s==1000'
> 1000000 loops, best of 3: 0.299 usec per loop

More results pretty much agree with yours:

C:\Python25>python -m timeit -s 'l=range(1000)' 'len(l)==1000'
10000000 loops, best of 3: 0.0235 usec per loop

C:\Python25>python -m timeit -s 'l=range(1000)' 'len(l)==1000'
10000000 loops, best of 3: 0.0245 usec per loop

C:\Python25>python -m timeit -s 'l=range(1000)' 's=len(l)' 's==1000'
10000000 loops, best of 3: 0.0383 usec per loop

C:\Python25>python -m timeit -s 'l=range(1000)' 's=len(l)' 's==1000'
10000000 loops, best of 3: 0.038 usec per loop



More information about the Python-list mailing list