472,961 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

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

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.

Now, *why* is such buffering gaining speed over stdio's fgets(), which
already does input buffering (though in a more subtle way, which makes
it still usable with pipes etc.) ?

-Alex

Jul 26 '07 #1
2 2939
Alexandre Ferrieux <al****************@gmail.comwrote:
Now, *why* is such buffering gaining speed over stdio's fgets(), which
already does input buffering (though in a more subtle way, which makes
it still usable with pipes etc.) ?
Because the C runtime library has different constraints than Python's file
iterator.

In particular the stdio fgets() must not read ahead (because the stream
might not be seekable), so it is usually just implemented as a series of
calls to read one character at a time until it has sufficient characters.
That inevitably has a lot more overhead than reading one 8k buffer and
subsequently splitting it up into lines.

It would probably be possible to do an implementation of fgets which looked
at the underlying stream and used buffering and seeking when the stream was
seekable and the cautious approach otherwise, but that isn't what is
usually done, and the incentive to do it isn't there: fgets() exists and
works as advertised even if it isn't very efficient. Anyone worried about
speed won't use it anyway, so improving it on specific platforms wouldn't
really help.

A lot of the C runtime is like that: it needs to be robust in a very
general purpose environment, but it doesn't need to be efficient. If you
are worried about efficiency then you should look elsewhere.
Jul 27 '07 #2
On Jul 27, 2:16 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Alexandre Ferrieux <alexandre.ferri...@gmail.comwrote:
Now, *why* is such buffering gaining speed over stdio's fgets(), which
already does input buffering (though in a more subtle way, which makes
it still usable with pipes etc.) ?

Because the C runtime library has different constraints than Python's file
iterator.

In particular the stdio fgets() must not read ahead (because the stream
might not be seekable), so it is usually just implemented as a series of
calls to read one character at a time until it has sufficient characters.
Sorry, but this is simply wrong. Try an strace on a process doing
fgets(), and you'll see that it does
large read()s. Moreover, the semantics of a blocking read on those
live sources is to block while there are no more data, but to return
whatever's available (possibly fewer bytes than asked) as soon as new
data arrive.

This is the key to libc's performance in both line-by-line or bulk
transfers.

In fact, the Python file iterator is not faster than fgets() by the
way. It's just faster than the Python-wrapped version of it, readline,
which you said is just a thin layer above fgets(). So instead of
dismissing too quickly ol'good libc, maybe the investigation could
uncover a slight suboptimality in readline...
Anyone worried about
speed won't use it anyway, so improving it on specific platforms wouldn't
really help.
Oh really ? grep, sed, awk, all these stdio-based tools, are pathetic
snails compared to the Python file iterator, of course. But then, why
didn't anyone feel the urge to use this very same trick in C and
provide an "fgets2()", working only on seekable devices, but so much
faster ?

-Alex
Jul 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
17
by: John Grandy | last post by:
What is the C# equivalent to the VB.Net looping structure: Protected rbl As RadioButtonList Dim li As ListItem For Each li In rbl.Items Next li
5
by: TJS | last post by:
trying to display pdf file in browser fails on this line: Response.ContentType = "application/pdf" getting an error about no declaration found for "response" what declaration is needed ???
12
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL...
10
by: Rafi B. | last post by:
I'm running this on a Linux/CentOs/cPanel server, trying to add caching to my PHP5 code, using ADOdb. I have two Linux servers, one of them is running perfect, the second one, just crashes without...
4
by: Jacob Rael | last post by:
I am new to python and I love it. I am hacking a file. I want to not print a line if it contains the word 'pmos4_highv'. I also don't want to print the next line. The following code works but it...
2
by: -Lost | last post by:
I have been watching code execution in various situations and I have noticed something. On the first test my example gave me obvious results. One method was far faster than the other. However,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.