range() is not the best way to check range?
Dan Bishop
danb_83 at yahoo.com
Mon Jul 17 23:09:00 EDT 2006
Leif K-Brooks wrote:
> Summercoolness at gmail.com wrote:
> > or is there an alternative use of range() or something similar that can
> > be as fast?
>
> You could use xrange:
>
> leif at ubuntu:~$ python -m timeit -n10000 "1 in range(10000)"
> 10000 loops, best of 3: 260 usec per loop
> leif at ubuntu:~$ python -m timeit -n10000 "1 in xrange(10000)"
> 10000 loops, best of 3: 0.664 usec per loop
That's only because you're choosing a number that's early in the list.
~$ python -m timeit -n10000 "1 in xrange(10000)"
10000 loops, best of 3: 1.22 usec per loop
~$ python -m timeit -n10000 "9999 in xrange(10000)"
10000 loops, best of 3: 1.24 msec per loop
That's *milliseconds*, not microseconds.
More information about the Python-list
mailing list