python disk i/o speed

nnes pruebauno at latinmail.com
Mon Aug 12 12:38:04 CEST 2002


bokr at oz.net (Bengt Richter) wrote in message news:<aj1jdh$q2d$0 at 216.39.172.122>...
> On 9 Aug 2002 22:42:14 GMT, bokr at oz.net (Bengt Richter) wrote:
> [...]
> >I wonder what this would do. Don't forget to assign local names outside the loop and use
> >them inside the loop. I.e., recapping (untested(!))
> import sys,xreadlines
> if len(sys.argv)<3:
>     sys.exit("bench input output")
> 
> input=open(sys.argv[1])
> output=open(sys.argv[2],"w")
> l_int = int
> l_add = int.__add__
> l_map = map
> l_reduce = reduce
> l_outwrite = output.write
> for line in input.xreadlines():
>     line12 = line[1:-2]
>     l_outwrite('"%s", "%s"\n' % (
>         line12,
>         l_reduce(l_add, l_map(l_int, line12.split('","'))))
>     )

reduce and map doesn´t seem to improve speed in this particular
situation. The line12 idea is faster though. Fastes implementation so
far on Python 2.2.1 Win2000 (28 seconds, was 36):

import sys,xreadlines

def filterfile2(inname, outname):

   input = file(inname)
   output = file(outname, 'w')

   localint = int
   l_outwrite = output.write

   for line in input.xreadlines():
       line12 = line[1:-2]
       x, y, z = line12.split('","')
       l_outwrite('"'+line12+'","'+str(localint(x)+localint(y)+localint(z))+'"\n
')

   output.close()
   input.close()

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

filterfile2(sys.argv[1],sys.argv[2])


---- Nestor



More information about the Python-list mailing list