python disk i/o speed

Bengt Richter bokr at oz.net
Fri Aug 9 19:32:01 EDT 2002


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():
>         l_outwrite('%s, "%s"\n' % (
>             line[:-1], # chop \n
>             l_reduce(l_add, l_map(l_int, line[1:-2].split('","')))))
                                                                     ^ extra paren
>         )
>
>Hm, slicing the line twice should be unnecessary overhead:
>
>     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('","')))))
                                                                 ^ extra paren
>         )
>
Just noticed. Also spurious indent. Sorry 'bout that. Could be other things too...
--
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('","'))))
    )
--

Regards,
Bengt Richter



More information about the Python-list mailing list