Lazy "for line in f" ?

Alexandre Ferrieux alexandre.ferrieux at gmail.com
Mon Jul 23 11:18:35 CEST 2007


On Jul 23, 10:33 am, Duncan Booth <duncan.bo... at invalid.invalid>
wrote:
>
> The extra buffering means that iterating over a file is about 3 times
> faster than repeatedly calling readline.
>
>     while 1:
>         line = f.readline()
>         if not line:
>             break
>
>     for line in f:
>         pass
>

Surely you'll notice that the comparison is spoilt by the fact that
the readline version needs an interpreted test each turn around.
A more interesting test would be the C-implemented iterator, just
calling fgets() (the thin layer policy) without extra 8k-blocking.

-Alex




More information about the Python-list mailing list