Ryan's District Boards

Computer, programming, and webmaster help , support , tips and tricks => Tutorials Zone! => Internet webmaster computer programming technology tips and tricks => C++ / C / C# ....Tutorials => Topic started by: ben2ong2 on October 06, 2006, 11:27:19 PM

Title: C++ Tips: IO - slow standard c++ I/O
Post by: ben2ong2 on October 06, 2006, 11:27:19 PM
(Newsgroup: comp.lang.c++.moderated, 4 May 2000)

[ This is part a discussion about why the C++ I/O facilities sometimes
  are slow (more in another tip). -adc ]

[snip]

AUSTERN: Matt Austern <austern@sgi.com>

The C++ iostreams library can be synchronized with the C stdio
library.  The C++ standard is slightly vague about what "synchronized"
means, but, from talking to the people who wrote that part of the
standard, the clear intent was that we were talking about a very tight
coupling.  For example, you should be able to read a character with
getchar(), then put it back with std::cin.rdbuf()->sputbackc(), and
then read it again either from stdin or from cin.

This tight coupling comes at a cost.  Now notice I said that the C++
iostreams library "can be" synchronized.  What this means is that
there are two modes for C++ I/O through the standard narrow stream
objects: a slow synchronized mode, and a fast unsynchronized mode.
In some implementations the speed difference between the two modes is
very large.

The C++ standard requires the iostreams library to use the slow
synchronized mode by default.  If you care about performance, and if
you don't plan to do a lot of mixing of stdio and iostreams, then
you should put the line
    std::sync_with_stdio(false);
in your program before doing any I/O.


_______________________________________________
cpptips mailing list
http://cpptips.hyperformix.com