Re: Why is "for line in f" faster than readline()

2007-07-27 Thread Alexandre Ferrieux
On Jul 27, 2:16 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: > Alexandre Ferrieux <[EMAIL PROTECTED]> wrote: > > Now, *why* is such buffering gaining speed over stdio's fgets(), which > > already does input buffering (though in a more subtle way, which makes &g

Why is "for line in f" faster than readline()

2007-07-26 Thread Alexandre Ferrieux
Hi, In a recent thread I discovered why the "for line in f" idiom was not suitable for live sources (pipes, sockets, tty). The reason is that it uses buffering on input, blocking on a full buffer read before anything. When I asked why it did it this way, the answer came up that it made it faster.

Re: Lazy "for line in f" ?

2007-07-23 Thread Alexandre Ferrieux
On Jul 23, 12:18 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: > > Whatever, the iterator makes the code both cleaner and faster. It is at > the expense of not being suitable for interactive sessions, or in some > cases pipes, but for those situations you can continue to use readline > and the extra

Re: Lazy "for line in f" ?

2007-07-23 Thread Alexandre Ferrieux
On Jul 23, 10:33 am, Duncan Booth <[EMAIL PROTECTED]> 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: >

Re: Lazy "for line in f" ?

2007-07-23 Thread Alexandre Ferrieux
On Jul 23, 9:36 am, Paul Rubin <http://[EMAIL PROTECTED]> wrote: > Alexandre Ferrieux <[EMAIL PROTECTED]> writes: > > So I'll reiterate the question: *why* does the Python library add that > > extra layer of (hard-headed) buffering on top of stdio's ? > >

Re: Lazy "for line in f" ?

2007-07-23 Thread Alexandre Ferrieux
On Jul 23, 1:03 am, Steve Holden <[EMAIL PROTECTED]> wrote: > > What makes you think Python doesn't use the platform fgets()? The fact that it does that extra layer of buffering. Stdio is already buffered, duplicating this is useless. > ... in the case of file.next() (the file method called to >

Re: Lazy "for line in f" ?

2007-07-22 Thread Alexandre Ferrieux
On Jul 22, 7:21 pm, Miles <[EMAIL PROTECTED]> wrote: > On 7/22/07, Alexandre Ferrieux wrote: > > > The Tutorial says about the "for line in f" idiom that it is "space- > > efficient". > > Short of further explanation, I interpret this as "do

Lazy "for line in f" ?

2007-07-22 Thread Alexandre Ferrieux
Hi, I'm a total newbie in Python, but did give quite a try to the documentation before coming here. Sorry if I missed the obvious. The Tutorial says about the "for line in f" idiom that it is "space- efficient". Short of further explanation, I interpret this as "doesn't read the whole file before