iostream performance

Bob Glickstein bobg@zanshin.com
Tue Feb 9 11:28:00 GMT 1999


Hi!  This isn't quite a bug, but: recently I did some tests comparing
the performance of this C program

  int c;

  while ((c = getchar()) != EOF)
    putchar(c);

with this C++ program

  int c;

  while ((c = cin.get()) != EOF)
    cout << static_cast<char>(c);

Here are the results in seconds, copying a 9 Mb file to /dev/null:

                       Pentium    PowerPC
                       ----       -----
C, non-optimized       6.377      1.94
C, optimized           6.139      1.181

C++, non-optimized     12.683     16.53
C++, optimized         10.698     10.339

(The Pentium has egcs 2.92.25 installed; the PowerPC, egcs 2.90.25.)

According to strace, the main loop of all versions makes precisely the
same system calls; further, compiling the C version with the C++
compiler produces C-like performance.  So the performance hit seems to
be in the iostream implementation.

My questions are: why is there such a performance penalty for using
iostreams; is there any way to work around that; and is there any
prospect for better performance in future versions?

Many thanks in advance.


More information about the Gcc-bugs mailing list