performance of script to write very long lines of random chars

Michael Torrie torriem at gmail.com
Wed Apr 10 21:52:22 EDT 2013


On 04/10/2013 07:21 PM, gry wrote:
> from sys import stdout
> from array import array
> import random
> nchars = 32000000
> rows = 10
> avail_chrs =
> '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&
> \'()*+,-./:;<=>?@[\\]^_`{}'
> a = array('c', 'X' * nchars)
> 
> for l in range(rows):
>     for i in xrange(nchars):
>         a[i] = random.choice(avail_chrs)
>     a.tofile(stdout)
>     stdout.write('\n')
> 
> This version using array took 4 min, 29 sec, using 34MB resident/110
> virtual. So, much smaller than the first attempt, but a bit slower.
> Can someone suggest a better code?  And help me understand the
> performance issues here?

Why are you using an array?  Why not just rely on the OS to buffer the
output.  Just write your characters straight to stdout instead of
placing them in an array.

At that point I believe this program will be as fast as is possible in
Python.



More information about the Python-list mailing list