getc() and fgetc() have fallen in a hole and can't get up.

paul.matthews at paul.matthews at
Thu Apr 11 11:01:28 EST 2002

I have a number of programs that are very IO bound (this is unavoidable). 
So I have been experimenting with different means of reading the data. I 
started off with the non-standard getline() function. This is the fastest, 
but when a POSIX'ly correct getline() replacement is substituted, 
performance fell into a major hole. 

The C++ getline function that comes with the iostreams classes is even 

Further experimentation shows that getc() is a major drag, far more than 
it should be. It also appears that getc() is no longer a macro.  The 
fread() routine also appears to be suboptimal speed wise now.

Since not wanting to double buffer though FILE* and then an though an 
internal buffer. I ported over the classes InputStream, 
FileInputStream, and BufferedInputStream to C++. The FileInputStream class 
talks directly to a file descriptor (int).

Despite using virtual functions, and lots of assertion code, the judicious 
use of inline allowed not only this implementation to beat getc(), but be 
POSIX'ly compatable, and do usefull things like throw exceptions on fatal 
read errors. 

Question: Why have the I/O routines become so slow?

[ Now all I need are a POSIX snprintf(), asprint() and I'll be right. ]

Paul Matthews
ETI Migrations
-------------- next part --------------
HTML attachment scrubbed and removed

More information about the linux mailing list