while (a=b()) ... (transmogrified to a range thread)

Jeremy Hylton jeremy at cnri.reston.va.us
Tue May 18 10:56:25 EDT 1999


>>>>> "TP" == Tim Peters <tim_one at email.msn.com> writes:

  TP> [Tim]
  >> You need to be much clearer about your claim here; certainly
  >> xrange(1000000) runs much faster than range(1000000) on anyone's
  >> machine (the former is constant time regardless of argument and
  >> the latter at best takes time proportional to a million), so your
  >> real complaint is about something else.

  TP> [Jeremy Hylton] [mailto:jeremy at cnri.reston.va.us]
  >> Actually, range(1000000) is faster on my machine.  Something like
  >> 3% faster, but still faster.  Of course, if the program can
  >> amortize the cost of creation/deletion across multiple
  >> iterations, it will be substantially faster.

TP> from time import clock
TP> N = 1000000
TP> start1 = clock(); x = xrange(N); finish1 = clock()
TP> start2 = clock(); y =  range(N); finish2 = clock()
TP> print "xrange time", finish1 - start1
TP> print " range time", finish2 - start2

Right.  I was timing an iteration over the range -- 
    for i in xrange(N): pass
vs.
    for i in range(N): pass
In this case, range nearly always wins -- even for N == 1000000.

I see now that your point was a bit different than I took it to be.
So the original comment about range was really a complaint about
something else.  

Jeremy




More information about the Python-list mailing list