I'm missing something here with range vs. xrange

John Machin sjmachin at lexicon.net
Fri Dec 7 17:09:25 EST 2007


On Dec 8, 8:08 am, "Joe Goldthwaite" <j... at goldthwaites.com> wrote:
> Here's the simple benchmark;
>
> start = time.time()
> for x in xrange(3):
>         for y in xrange(10000000):
>                 pass
> print 'xRange %s' % (time.time() - start)
>
> start = time.time()
> for x in range(3):
>         for y in range(10000000):
>                 pass
> print 'Range %s' % (time.time() - start)
>
> Here's what I get;
>
> xRange 92.5529999733
> Range 95.2669999599
>
> Not a lot of difference.  Range is slower but not by much.

90+ seconds?? What hardware, OS, and Python version? What else was
running in the background?

With this kit:
OS Name:	Microsoft Windows XP Professional
Version:	5.1.2600 Service Pack 2 Build 2600
Processor:	x86 Family 15 Model 36 Stepping 2 AuthenticAMD ~1995 Mhz
Python: Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310
32 bit (Intel)] on win32

and your exact code, I get:
xRange 4.0
Range 5.0

After changing time.time to time.clock, I get:
xRange 4.00560127055
Range 4.8927366467

After making the "benchmark" somewhat more realistic by executing it
inside a function, I get:
xRange 1.86865816745
Range 3.31902658019

By "inside a function", I mean changing
<script>
to

def foo():
    <script>
foo()




More information about the Python-list mailing list