Python example: possible speedup?

Fredrik Lundh fredrik at pythonware.com
Thu Sep 9 03:22:23 EDT 1999


Hrvoje Niksic <hniksic at srce.hr> wrote:
> > - reading a file one line at a time (self.__fp.readline())
> 
> I don't see an alternative to this, except to read the whole file at
> once, which I am trying to avoid, as the files are large.

note that:

    lines = fp.readlines(16384)
    if not lines:
        break
    for line in lines:
        ...

is usually much faster than

    line = fp.readline()
    if not line:
        break
    ...

(adjust the readlines buffer size as suitable)

but sure, Python's standard file object is slow.
working on a replacement (as an image processing
guy, I've never understood why anyone ever used
stdio), but there's so much code to write, and
sooo little time....

</F>





More information about the Python-list mailing list