 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Debajit Adhikary Guest
|
Posted: Mon Oct 18, 2004 8:13 pm Post subject: 'n' vs std::endl for Displaying Newlines |
|
|
(1) In what cases is std::endl better than a trailing n for
displaying a newline, for instance, at the end of some std::cout
statement? (I'm looking for concrete examples)
(2) Which all buffers does std::endl flush?
(3) Is something like
std::cout << "some textn";
*always* faster than
std::cout << "some text" << std::endl;
?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Tue Oct 19, 2004 4:34 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Debajit Adhikary wrote:
| Quote: | (1) In what cases is std::endl better than a trailing n for
displaying a newline, for instance, at the end of some std::cout
statement? (I'm looking for concrete examples)
|
It's only necessary if you want to see the output and there is some
time that passes before what another buffer flush. However, it's
rare that makes a difference. The few times you really care, like
you're about to do some input, already force a flush otherwise.
| Quote: |
(2) Which all buffers does std::endl flush?
|
It flushes the stream buffer associated with whatever ostream
you invoke it on.
| Quote: |
(3) Is something like
std::cout << "some textn";
*always* faster than
std::cout << "some text" << std::endl;
It's not ALWAYS faster, but it probably never will never be slower. |
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Milivoj Davidov Guest
|
Posted: Tue Oct 19, 2004 6:00 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
"Debajit Adhikary" <debajit1 (AT) gmail (DOT) com> wrote
| Quote: | (1) In what cases is std::endl better than a trailing n for
displaying a newline, for instance, at the end of some std::cout
statement? (I'm looking for concrete examples)
|
See James Kanze's first reply to your original posting.
I'd also say that std::endl is always better than 'n'
(except when you are optimizing known performance bottlenecks).
Std::endl provides expected and correct behaviour,
especially in cases of applications with multiple
processes, e.g. back-end, unattended servers.
Using 'n' is an optimisation technique, it can lead to loss of
data, and data out of order; its performance is less predictable.
| Quote: | (2) Which all buffers does std::endl flush?
|
See first reply to your OP.
For example: std::cout << std::endl flushes std::cout's buffer,
whereas mystream << std::endl flushes mystream's buffer
(assuming mystream is an std::ostream, or std::ofstream, etc.).
| Quote: | (3) Is something like
std::cout << "some textn";
*always* faster than
std::cout << "some text" << std::endl;
?
|
See first reply to your OP. (No, not always.)
Additionally, 'n' is not faster when a buffer flush occurs
at its write (with no buffer flush it should be *much* faster).
Milivoj Davidov
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
hari4063 Guest
|
Posted: Wed Oct 20, 2004 4:14 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Main difference is that std::endl DOES flushing, and n does not. If You
look at implementation, endl is not defined like string, it is function (I
think). So, it can be faster to write new line with "n".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|