OSDN Git Service

5e668931c7d8601e87a5e4ddb908cd9497674e45
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / html / manual / bk01pt11ch28s02.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Performance</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="io_and_c.html" title="Chapter 28. Interacting with C" /><link rel="prev" href="io_and_c.html" title="Chapter 28. Interacting with C" /><link rel="next" href="extensions.html" title="Part XII.  Extensions" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Performance</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="io_and_c.html">Prev</a> </td><th width="60%" align="center">Chapter 28. Interacting with C</th><td width="20%" align="right"> <a accesskey="n" href="extensions.html">Next</a></td></tr></table><hr /></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.io.c.sync"></a>Performance</h2></div></div></div><p>
4       Pathetic Performance? Ditch C.
5     </p><p>It sounds like a flame on C, but it isn't.  Really.  Calm down.
6       I'm just saying it to get your attention.
7    </p><p>Because the C++ library includes the C library, both C-style and
8       C++-style I/O have to work at the same time.  For example:
9    </p><pre class="programlisting">
10      #include &lt;iostream&gt;
11      #include &lt;cstdio&gt;
12
13      std::cout &lt;&lt; "Hel";
14      std::printf ("lo, worl");
15      std::cout &lt;&lt; "d!\n";
16    </pre><p>This must do what you think it does.
17    </p><p>Alert members of the audience will immediately notice that buffering
18       is going to make a hash of the output unless special steps are taken.
19    </p><p>The special steps taken by libstdc++, at least for version 3.0,
20       involve doing very little buffering for the standard streams, leaving
21       most of the buffering to the underlying C library.  (This kind of
22       thing is tricky to get right.)
23       The upside is that correctness is ensured.  The downside is that
24       writing through <code class="code">cout</code> can quite easily lead to awful
25       performance when the C++ I/O library is layered on top of the C I/O
26       library (as it is for 3.0 by default).  Some patches have been applied
27       which improve the situation for 3.1.
28    </p><p>However, the C and C++ standard streams only need to be kept in sync
29       when both libraries' facilities are in use.  If your program only uses
30       C++ I/O, then there's no need to sync with the C streams.  The right
31       thing to do in this case is to call
32    </p><pre class="programlisting">
33      #include <span class="emphasis"><em>any of the I/O headers such as ios, iostream, etc</em></span>
34
35      std::ios::sync_with_stdio(false);
36    </pre><p>You must do this before performing any I/O via the C++ stream objects.
37       Once you call this, the C++ streams will operate independently of the
38       (unused) C streams.  For GCC 3.x, this means that <code class="code">cout</code> and
39       company will become fully buffered on their own.
40    </p><p>Note, by the way, that the synchronization requirement only applies to
41       the standard streams (<code class="code">cin</code>, <code class="code">cout</code>,
42       <code class="code">cerr</code>,
43       <code class="code">clog</code>, and their wide-character counterparts).  File stream
44       objects that you declare yourself have no such requirement and are fully
45       buffered.
46    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="io_and_c.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="io_and_c.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="extensions.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 28. Interacting with C </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Part XII. 
47   Extensions
48   
49 </td></tr></table></div></body></html>