Why I think range is a wart.

Tim Legant tim-dated-1016821077.7a095e at catseye.net
Fri Mar 15 13:17:56 EST 2002


Peter Dobcsanyi <dpeter at designtheory.org> writes:

> Oops, you are right! I fixed it and ran the test again.
> 
> 3000 times, Python 2.2, Linux:
> 
>     i in range(len(ls)):             2.03
>     x in ls; i+= :                   2.28
>     lambda x:zip(range(len(x)), x):  3.76
>     Iter class:                     10.18
> 
> 10000 times:
> 
>     i in range(len(ls)):             6.68
>     x in ls; i+=1 :                  7.55
>     lambda x:zip(range(len(x)), x): 12.56
>     Iter class:                     35.56
> 
> Interestingly, "range(len(ls))" is still faster.

Cool.  And you're right, it's very interesting.  I wonder why /F
thought the bar() version would be faster.  The results seem to
suggest that adding 1 to a number is slower than indexing a list (the
one generated by range).


Tim




More information about the Python-list mailing list