get-a-cup-of-coffee slow

Grant Griffin not.this at seebelow.org
Thu Aug 9 16:24:03 EDT 2001


Hi Gang,

I tried the following today:

   def gen_sectors1():
       print 'Generating (Version 1)...',
       s = '' 
       for i in xrange(65536):
           s += chr(i & 0xFF)
       for i in xrange(65536):
           s += chr((65535 - i) & 0xFF);
       print 'done'
       return s

I knew from the get-go that this wasn't the fastest thing in the world, but I
was _quite_ surprised that it took about a minute-and-a-half on an 800 MHz
machine.

I then rewrote it as:

   def gen_sectors2):
       print 'Generating (Version 2)...',
       s = ''
       forward = ''
       reverse = ''
       for i in xrange(256):
           forward += chr(i)
           reverse += chr(255 - i)
       s += forward * 256
       s += reverse * 256
       print 'done'
       return s

which produces the same data, yet runs so fast as to seem instantaneous.  (I
doubt that I saved a minute-and-a-half by rewriting it, though <wink>.)

Why is the first one so slow?  I assume it has to do with repeatedly appending
characters to a string.  If so, is it because "s +=" is implemented as "s = s
+", where the new one is always an appended copy of the original?  (I guess I
would hope that in the case of a string "+=" operation, Python would leave a
little spare space at the end of the new string so multiple appends would not
have to make a copy with _each_ append.)

Also, I tried writing something like this with a list (which are presumably more
suited to being appended), but then I realized that I didn't know how to write
the contents of a list to a file as a stream of bytes.  There's probably a
simple way to do that: what is it?

not-used-to-being-surprised-by-python-ly y'rs,

=g2

_____________________________________________________________________

Grant R. Griffin                                       g2 at dspguru.com
Publisher of dspGuru                           http://www.dspguru.com
Iowegian International Corporation            http://www.iowegian.com




More information about the Python-list mailing list