 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Debajit Adhikary Guest
|
Posted: Wed Oct 13, 2004 9:21 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 |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Oct 14, 2004 10:16 am 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)
|
In general, prefer std::endl. Using 'n' is an optimization, not always
valide, and in general, should never be applied until the profiler says
so.
The one exception is when outputting a block of fixed lines, something
like:
std::cout << "Wanring:n You should have known better" << std::endl ;
It's OK to use the 'n' within a single statement, provided the
statement ends with a std::endl or a std::flush.
| Quote: | (2) Which all buffers does std::endl flush?
|
The one you write it to, of course.
| Quote: | (3) Is something like
std::cout << "some textn";
*always* faster than
std::cout << "some text" << std::endl;
?
|
Always, no. If the stream isn't buffered, there should be no
significant difference.
In practice, you usually only have two choices: unbuffer the streams, or
use std::endl systematically. Typically, std::endl is faster.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Thu Oct 14, 2004 10:19 am 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 makes a difference when you want to flush the stream, because std::endl
adds a 'n' and flushes.
| Quote: | (2) Which all buffers does std::endl flush?
|
It flushes the buffer of the stream, or do I misinterpret you question? You
also need to be aware of what std::ios_base::sync_with_stdio() does, it
influences the overall buffering and thus also performance.
| Quote: | (3) Is something like
std::cout << "some textn";
*always* faster than
std::cout << "some text" << std::endl;
?
|
I daresay yes, but they don't do the same thing. You should compare
out << "text" << endl;
with
out << "textn" << flush;
Of these, I don't think any is much faster than the other, but the second
one could be much more expressive.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Oct 14, 2004 8:35 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
In article <d6652001.0410132339.68076e8b (AT) posting (DOT) google.com>,
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes
| Quote: | "Debajit Adhikary" <debajit1 (AT) gmail (DOT) com> wrote in message
news:<1097664718.482270.300410 (AT) f14g2000cwb (DOT) googlegroups.com>...
(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)
In general, prefer std::endl. Using 'n' is an optimization, not always
valide, and in general, should never be applied until the profiler says
so.
|
At one time I would have agreed with you about preferring std::endl to
'n' but that is no longer the case.
Using endl is potentially a pessimisation because it forces a flush even
if it is unnecessary. In the case of std::cout there is rarely an
advantage in using std::endl because of the tie between it and std::cin.
Just possibly you might need to flush the output at the end of the
program or in a case where you want to warn the user that there may be a
long pause without visible activity. However making these explicit with
a call to flush() seems appropriate in these circumstances.
The problem is that once programmers get used to using endl with
std::cout they continue the practice when using other stream objects. In
those cases unnecessary flushing can have serious adverse effects. (E.g.
creating many seriously undersize packages of data transmitted over a
serial line.)
| Quote: |
The one exception is when outputting a block of fixed lines, something
like:
std::cout << "Wanring:n You should have known better" << std::endl ;
It's OK to use the 'n' within a single statement, provided the
statement ends with a std::endl or a std::flush.
|
It is always, IMHO, OK to use 'n' and it is sometimes not OK to use
std::endl instead.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ 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: Thu Oct 14, 2004 9:30 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | In general, prefer std::endl. Using 'n' is an optimization, not always
valide, and in general, should never be applied until the profiler says
so
|
I disagree. I say prefer 'n' unless you have explicit knowledge that
you need to flush the buffer. There is scant little need to ever explicitly
flush an ostream, and even when there is, it's often not always aligned
with the output of a newline.
| Quote: | In practice, you usually only have two choices: unbuffer the streams, or
use std::endl systematically. Typically, std::endl is faster.
|
No the other choice is to not thrown in unnecessary flushes at all, which
is even faster.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Thu Oct 14, 2004 9:49 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Hi!
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | In general, prefer std::endl. Using 'n' is an optimization, not always
valide, and in general, should never be applied until the profiler says
so.
|
I thought the opposite was true: prefer 'n' over endl. Because
if you use endl too often, then you virutally disable the
buffering, which could e.g. improve writing to files
significantly. And cout can easily be redirected to a file.
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Oct 15, 2004 9:53 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In article <d6652001.0410132339.68076e8b (AT) posting (DOT) google.com>,
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes
"Debajit Adhikary" <debajit1 (AT) gmail (DOT) com> wrote in message
news:<1097664718.482270.300410 (AT) f14g2000cwb (DOT) googlegroups.com>...
(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)
In general, prefer std::endl. Using 'n' is an optimization, not
always valide, and in general, should never be applied until the
profiler says so.
At one time I would have agreed with you about preferring std::endl to
'n' but that is no longer the case.
Using endl is potentially a pessimisation because it forces a flush
even if it is unnecessary. In the case of std::cout there is rarely an
advantage in using std::endl because of the tie between it and
std::cin. Just possibly you might need to flush the output at the end
of the program or in a case where you want to warn the user that there
may be a long pause without visible activity. However making these
explicit with a call to flush() seems appropriate in these
circumstances.
The problem is that once programmers get used to using endl with
std::cout they continue the practice when using other stream
objects. In those cases unnecessary flushing can have serious adverse
effects. (E.g. creating many seriously undersize packages of data
transmitted over a serial line.)
|
I think you'd agree with me that if the programmer is advanced enough to
understand the issues concerning buffering, he can (and will) make the
correct choice; he doesn't need a guideline.
If someone has to ask the question, however, then he isn't that
advanced, and doesn't know how to make the correct choice. And
std::endl, while it may be slower, will never be wrong.
In practice, there is an additional very strong advantage in using
std::endl. If your program crashes, you can only see from the external
files how far it got if you use std::endl. In my mind, that advantage
outweighs any optimization concerns. Until the profiler or the programs
performance tells me otherwise.
| Quote: | The one exception is when outputting a block of fixed lines,
something like:
std::cout << "Wanring:n You should have known better" << std::endl ;
It's OK to use the 'n' within a single statement, provided the
statement ends with a std::endl or a std::flush.
It is always, IMHO, OK to use 'n' and it is sometimes not OK to use
std::endl instead.
|
If you need the flush, 'n' alone is not OK. If you don't need it, the
only disadvantage of having it is performance.
In sum, it's an optimization trick, nothing else.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vyacheslav Kononenko Guest
|
Posted: Fri Oct 15, 2004 11:01 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Frank Birbacher wrote:
| Quote: | Hi!
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
In general, prefer std::endl. Using 'n' is an optimization, not always
valide, and in general, should never be applied until the profiler says
so.
I thought the opposite was true: prefer 'n' over endl. Because
if you use endl too often, then you virutally disable the
buffering, which could e.g. improve writing to files
significantly. And cout can easily be redirected to a file.
Frank
|
I don't agree with you Frank. Maybe it is more related to the "unix"
world, but imagine such situation: you made a program and some user
reported that it crashes on some set of data. What is the first step to
find the problem? Ask user to redirect output and send it to you (with
logs if your program provides them). So what will you see in that output
if you used 'n' instead of std::endl? How about logs? Do you have
different habbits on logs than on "just output"?
Remember: premature optimization is the root of all evil.
--
Regards,
Slava
[ 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 5:59 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in message
news:<d6652001.0410150136.55c88af5 (AT) posting (DOT) google.com>...
| Quote: |
In practice, there is an additional very strong advantage in using
std::endl. If your program crashes, you can only see from the external
files how far it got if you use std::endl. In my mind, that advantage
outweighs any optimization concerns. Until the profiler or the programs
performance tells me otherwise.
|
Spot on, I agree. Additionally, if more than one process
is writing into the same stream (e.g. log file) then std::endl will
preserve relative ordering of data written by different processes.
Messages written earlier by one process will correctly appear
before messages written later by another process.
With 'n' some very old messages from one process may / will
actually be written (flushed) after very recent messages from another
process - and this is completely unexpected and usually incorrect.
| Quote: | If you need the flush, 'n' alone is not OK. If you don't need it, the
only disadvantage of having it is performance.
In sum, it's an optimization trick, nothing else.
|
And early optimization is the root of all evil! )
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 |
|
 |
Seungbeom Kim Guest
|
Posted: Wed Oct 20, 2004 9:38 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | In practice, there is an additional very strong advantage in using
std::endl. If your program crashes, you can only see from the external
files how far it got if you use std::endl. In my mind, that advantage
outweighs any optimization concerns. Until the profiler or the programs
performance tells me otherwise.
|
In <http://www.aristeia.com/Papers/C++ReportColumns/novdec95.pdf>,
Eric Nagler is told to have said:
| Quote: | The user is going to have the buffer flushed, like it or not,
because of the fact that the ios::unitbuf bit is turned on
by default. So using 'n' vs. endl is a moot point. If the
buffer really should not be flushed, then this bit needs to be
turned off.
|
And the author says:
| Quote: | I noticed something that took me by surprise: cout is supposed
to be unbuffered. You may not find this to be a great shock,
but traditionally cout has been a buffered ostream. For unbuffered
output, the traditional stream of choice has been cerr. It seems
the ANSI/ISO committee decided to part with tradition.
|
This is written in 1995, so it's quite old, but it led the author to
abandon the whole item from his book at that time.
Does this still hold in the current standard?
| Quote: | If you need the flush, 'n' alone is not OK. If you don't need it, the
only disadvantage of having it is performance.
In sum, it's an optimization trick, nothing else.
|
By the way, since when in the era of C programming has it been
considered as an optimization to write this:
printf("Hello, World!n");
printf("Do you see this?n");
instead of this?
printf("Hello, World!n"); fflush(stdout);
printf("Do you see this?n"); fflush(stdout);
We've been used to not using fflush() every time we put a newline,
haven't we? Was it an optimization? Am I missing something?
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Markus Elfring Guest
|
Posted: Thu Oct 21, 2004 5:13 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
| 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?
|
How do you treat the variations in line endings of the different operating
systems?
See also topic "Should I end my output lines with std::endl or 'n'?".
http://parashift.com/c++-faq-lite/input-output.html#faq-15.7
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Oct 21, 2004 5:30 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Seungbeom Kim <musiphil (AT) bawi (DOT) org> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote:
In practice, there is an additional very strong advantage in using
std::endl. If your program crashes, you can only see from the
external files how far it got if you use std::endl. In my mind,
that advantage outweighs any optimization concerns. Until the
profiler or the programs performance tells me otherwise.
In <http://www.aristeia.com/Papers/C++ReportColumns/novdec95.pdf>,
Eric Nagler is told to have said:
The user is going to have the buffer flushed, like it or not,
because of the fact that the ios::unitbuf bit is turned on by
default. So using 'n' vs. endl is a moot point. If the buffer
really should not be flushed, then this bit needs to be turned off.
And the author says:
I noticed something that took me by surprise: cout is supposed to be
unbuffered. You may not find this to be a great shock, but
traditionally cout has been a buffered ostream. For unbuffered
output, the traditional stream of choice has been cerr. It seems the
ANSI/ISO committee decided to part with tradition.
This is written in 1995, so it's quite old, but it led the author to
abandon the whole item from his book at that time.
Does this still hold in the current standard?
|
There is no unitbuf bit in the standard.
Traditionally, in older implementations, standard out was unbuffered
when outputting to an interactive device. Which doesn't say anything
about outputting to a file.
In the standard, the solution adopted was that standard in is "tied" to
standard out -- any attempt to read from standard in will flush standard
out. Again, this has no effect if 1) you're not reading from standard
in, or 2) you're not using standard out.
| Quote: | If you need the flush, 'n' alone is not OK. If you don't need it,
the only disadvantage of having it is performance.
In sum, it's an optimization trick, nothing else.
By the way, since when in the era of C programming has it been
considered as an optimization to write this:
printf("Hello, World!n");
printf("Do you see this?n");
instead of this?
printf("Hello, World!n"); fflush(stdout);
printf("Do you see this?n"); fflush(stdout);
We've been used to not using fflush() every time we put a newline,
haven't we? Was it an optimization? Am I missing something?
|
Yep. C has something called line buffering, which can be used to cause
an automatic flush on each 'n'.
More to the point, of course, C has a somewhat different culture. If
you use C, you're supposed to suffer:-).
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sergei Zyablov Guest
|
Posted: Thu Oct 21, 2004 7:40 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
Seungbeom Kim <musiphil (AT) bawi (DOT) org> wrote
| Quote: | By the way, since when in the era of C programming has it been
considered as an optimization to write this:
printf("Hello, World!n");
printf("Do you see this?n");
instead of this?
printf("Hello, World!n"); fflush(stdout);
printf("Do you see this?n"); fflush(stdout);
We've been used to not using fflush() every time we put a newline,
haven't we? Was it an optimization? Am I missing something?
Only humor is black humor... |
You've struck me, so I decide to add yet another argument to this
(shallow?) discussion.
Right now I have to program on Ruby having the << operator but no
endl.
I tend to use this strange construction:
$stdout << "These long lines of text" << "n" <<
" to be edited in the future." << "n";
$stdout.flush();
Instead of trying to explain why, I suggest to follow me for two
months and then roll back to "Textn" style...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Xenos Guest
|
Posted: Fri Oct 22, 2004 5:17 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
"Markus Elfring" <Markus.Elfring (AT) web (DOT) de> wrote
| Quote: | How do you treat the variations in line endings of the different operating
systems?
You don't need to worry about it. When you output a 'n', your program |
will--compiled for that particular OS--output the correct line ending.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seanairt Guest
|
Posted: Fri Oct 22, 2004 5:39 pm Post subject: Re: 'n' vs std::endl for Displaying Newlines |
|
|
On Thu, 14 Oct 2004 06:16:58 -0400, kanze wrote:
| Quote: | In general, prefer std::endl. Using 'n' is an optimization, not always
valide, and in general, should never be applied until the profiler says
so.
|
Herb Sutter's latest book, Exceptional C++ Style, states exactly
the opposite (item #1 on use of std::vector). Since he's taken the time to
write about it there, I'll simply refer you to said text instead of
parroting what he's written.
[ 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
|
|