range question, compared to Smalltalk

Neal Norwitz neal at metaslash.com
Wed Aug 28 19:11:59 EDT 2002


On Wed, 28 Aug 2002 12:13:40 -0400, Frank Buss wrote:

> "Bjorn Pettersen" <BPettersen at NAREX.com> wrote:
> 
>> That, of course, depends <wink>. On my machine range is faster for
>> lists up to ~1500 items and comparable to xrange up to ~17000 items...
>> Test program below.
> 
> Do you know the german saying "Wer misst misst Mist" (who measures
> measures muck) ? :-)
> 
> Looks like it's highly optimized for loops and I can use it for this
> purpose.  But lets try this code:

[snip]

I tried both programs with the current version of Python and xrange
seems to be the clear winner:

[neal at epoch src]$ ./python t2.py 
    9999 6.560 0.230          xrange

[neal at epoch src]$ ./python t2.py > n
# I stopped this after a while :-)
[neal at epoch src]$ grep -c ' range' n 
1727
[neal at epoch src]$ grep -c 'xrange' n 
9407
[neal at epoch src]$ wc n
  11134   44536  411958 n

range rarely won, first winners were 141, 204, 254, 524, ...

But if you really want to make it go fast, use psyco. :-)

Neal



More information about the Python-list mailing list