get-a-cup-of-coffee slow (fwd)

Grant Griffin not.this at
Fri Aug 10 16:50:17 CEST 2001

In article <3B73E1BB.6825C31A at>, Grant says...
>Michael Linnemann wrote:
>> And here is the result:
>>   Version 2: 0.006
>>   Version 3: 1.005
>>   Version 4: 0.054

Here's my results.  (Some new versions are also benchmarked below):

Using repeated string "+=":

   Generating (Version 1)... done
   1.507583 minutes

Using string multiplication (this is by far the fastest one yet):

   Generating (Version 2)... done
   0.000098 minutes

Using list "+=" of "[chr()]":

   Generating (Version 3a)... done
   0.013425 minutes

Using list append of "chr" (which is slightly faster):

   Generating (Version 3b)... done
   0.012451 minutes

Using list comprehensions (faster yet):

    s = [chr(i & 0xFF) for i in xrange(65536)]
    s += [chr((65535 - i) & 0xFF) for i in xrange(65536)]

   Generating (Version 3c)... done
   0.009393 minutes

Using array.array('c'):

   Generating (Version 4a)... done
   0.008935 minutes

Using an array initialized via a list comprehension (I thought this might be
faster because it would "pre-allocate" the array, but it wasn't):

   s = array.array('c', [chr(0) for x in xrange(2 * 65536)])

   Generating (Version 4b)... done
   0.016283 minutes

   -for-fear-my-head-will-explode-<wink>-ly y'rs,



Grant R. Griffin                                       g2 at
Publisher of dspGuru                 
Iowegian International Corporation  

More information about the Python-list mailing list