Re: range() is not the best way to check range?



Leif K-Brooks wrote:
Summercoolness@xxxxxxxxx wrote:
or is there an alternative use of range() or something similar that can
be as fast?

You could use xrange:

leif@ubuntu:~$ python -m timeit -n10000 "1 in range(10000)"
10000 loops, best of 3: 260 usec per loop
leif@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.

.



Relevant Pages