C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

'n' vs std::endl for Displaying Newlines

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Debajit Adhikary
Guest





PostPosted: Mon Oct 18, 2004 8:13 pm    Post subject: 'n' vs std::endl for Displaying Newlines Reply with 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)

(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





PostPosted: Tue Oct 19, 2004 4:34 pm    Post subject: Re: 'n' vs std::endl for Displaying Newlines Reply with quote



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





PostPosted: Tue Oct 19, 2004 6:00 pm    Post subject: Re: 'n' vs std::endl for Displaying Newlines Reply with quote



"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





PostPosted: Wed Oct 20, 2004 4:14 pm    Post subject: Re: 'n' vs std::endl for Displaying Newlines Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.