# [Tutor] Why does counting to 20 million stress my computer?

Dick Moores rdm at rcblue.com
Sun Aug 1 14:20:48 CEST 2004

```At 14:41 7/20/2004, Alan Gauld wrote:
> > So xrange is definitely quicker. Is it better to use xrange all the
>time in
> > place of range then ?
>
>I haven't checked but my guess is that for small values range will be
>faster
>because it holds all the values in RAM and indexing will be faster
>than
>calculation.
>
>But if in doubt try it out...
>And tell us the result!

Thanks to Kent Johnson's post of 7/29 I discovered the timeit module, and
was able to compare counting with range() vs. counting with xrange() for
much smaller numbers than a million. Alan Gauld's guess is correct:
range() is faster counting to numbers less than 30,000 or so; however,
from just below 1000 down to 1, xrange() is faster.

Here the script I used:

=====================================
#SpinSpeeds.py

def countUsingRange(n):
for i in range(n):
pass

def countUsingXrange(n):
for i in xrange(n):
pass

if __name__=='__main__':
from timeit import Timer
repetitions = 1000
n = 25000

t = Timer("countUsingRange(25000)", "from __main__ import
countUsingRange")
rangeTime = t.timeit(repetitions)

t = Timer("countUsingXrange(25000)", "from __main__ import
countUsingXrange")
xrangeTime = t.timeit(repetitions)

print " range(%d) time = %f for %d repetitions" % (n, rangeTime,
repetitions)
print "xrange(%d) time = %f for %d repetitions" % (n, xrangeTime,
repetitions)
==========================================

This gets:
range(30000) time = 2.902619 for 1000 repetitions
xrange(30000) time = 3.064199 for 1000 repetitions

some other results:

range(10000000) time = 12.296007 for 10 repetitions
xrange(10000000) time = 9.133722 for 10 repetitions

range(1000000) time = 11.747225 for 100 repetitions
xrange(1000000) time = 10.285452 for 100 repetitions

range(100000) time = 11.334980 for 1000 repetitions
xrange(100000) time = 10.022997 for 1000 repetitions

range(50000) time = 10.775690 for 2000 repetitions
xrange(50000) time = 9.947329 for 2000 repetitions

range(40000) time = 8.305034 for 2000 repetitions
xrange(40000) time = 8.039629 for 2000 repetitions

range(20000) time = 7.343917 for 4000 repetitions
xrange(20000) time = 7.936993 for 4000 repetitions

range(10000) time = 8.892325 for 10000 repetitions
xrange(10000) time = 9.650432 for 10000 repetitions

range(1000) time = 8.960758 for 100000 repetitions
xrange(1000) time = 10.031941 for 100000 repetitions

range(750) time = 10.103314 for 150000 repetitions
xrange(750) time = 10.909828 for 150000 repetitions

range(500) time = 9.309678 for 200000 repetitions
xrange(500) time = 8.274005 for 200000 repetitions

range(100) time = 10.556858 for 1000000 repetitions
xrange(100) time = 8.477174 for 1000000 repetitions

range(10) time = 29.615361 for 10000000 repetitions
xrange(10) time = 21.842709 for 10000000 repetitions

range(5) time = 12.460505 for 5000000 repetitions
xrange(5) time = 9.244912 for 5000000 repetitions

range(1) time = 10.714814 for 5000000 repetitions
xrange(1) time = 7.521607 for 5000000 repetitions

Dick Moores

```