.readline() - VERY SLOW compared to PERL

Alex Martelli aleaxit at yahoo.com
Tue Nov 21 03:36:53 EST 2000


"Harald Schneider" <h_schneider at marketmix.com> wrote in message
news:8vd94d$mjo$06$1 at news.t-online.com...
    [snip]
> #!/usr/bin/perl
    [snip]
> Result of upper Script: 3.705 sec
    [snip]
> while 1:
>     lines = db.readlines(250000)
>     if not lines:
>         break
>     for dbline in lines:
>    rs = string.split(dbline, ';')
>
>    if rs[0] == 'TEST':
>     print dbline + "\n"
>     break
    [snip]
> Result of upper Script: 5.629 sec.

I wonder if a tad extra performance might not be obtained
in the Python version by a slightly more Pythonic idiom in
the inner loop, e.g. (in Python 2.0):

    proceed = 1
    while proceed:
        lines = db.readlines(250000)
        if not lines: break
        for dbline in lines:
            if dbline.startswith('TEST'):
                print dbline,'\n'
                proceed = 0
                break

(this also has the same semantics as the Perl script,
which, it seems to me, your original Python version does
not, since the 'break' only breaks from one level of
loop; I inserted the 'proceed' switch as one way to
ensure termination of the outer-loop as well).


Alex






More information about the Python-list mailing list