python disk i/o speed

nnes pruebauno at
Thu Aug 8 13:17:30 CEST 2002

Thanks all of you!

After reading the enlightening comments I came up with a new version.
It looks nicer too :-).

Jeffs code looked like out of a Cookbook: use re for input and % for
output on stdio and you have got almost unlimited flexibility, and if
you understand regular expresions you know exactly what´s going on. It
was slower of course. Now I wish the print format syntax and the re
syntax were somehow unified.

The other approaches were similar to mine using split. Some also using
map and reduce which would be good if I had more than 3 variables. I
will test it later, but I doubt it will improve the speed.

Unfortunately the speedup is not near I would have expected. From the
old 36 seconds down to 33 seconds (using python -O). So I am still
open to suggestions.

cheers all

import sys,xreadlines

def filterfile2(inname, outname):

     inp = file(inname)
     out = file(outname, 'w')

     localint = int

     for line in inp.xreadlines():
         x, y, z = line.split('","')
         out.write('"%s","%s","%s","%i"\n' % (
          x,y,z,localint(x) + localint(y) + localint(z)))

if len(sys.argv)<3:
    sys.exit("bench input output")


More information about the Python-list mailing list