[Python-Dev] Comparison speed
Tim Peters
tim.one@home.com
Wed, 16 May 2001 04:51:17 -0400
[Barry A. Warsaw]
> ...
> from types import StringType
> import time
> r = range(1000000)
>
> def one(r=r):
> x = 'hello'
> t0 = time.time()
> for i in r:
Random clue: when you're too lazy to try to subtact out loop overhead (not a
knock, I am too), you may have better luck with
r = [1] * 1000000
than
r = range(1000000)
The reason is that the former way gets to keep incref'ing and decref'ing a
single object (as it's repeatedly bound to "i" across iterations), instead of
slobbering all over memory inc'ing and dec'ing a million distinct objects.
there's-as-an-art-to-doing-nothing-quickly-ly y'rs - tim