Boost logo

Boost :

From: Dietmar Kuehl (dietmar_kuehl_at_[hidden])
Date: 2000-03-07 09:53:39


Hi,

> > Providing a nl encourages the code that is the equivalent of:
> > cout << "First line" << "\n" << "Second line";
>
> Agreed... although, is it that big a slowdown?

To be sure, you would hage to measure it. From a conceptual perspective
there is indeed quite some stuff to be done which is not really needed.
The slowdown has multiple reasons:

- Each output operator create a 'std::ostream::sentry' object.
  Although this should be rather fast and most of the involved work
  might be avoided by a clever compiler, this not for free. Creating
  only one such object is clearly cheaper.

- The function for determining the string length is called twice. Since
  this function works better the longer the strings are, this might be
  a notable difference.This can be somewhat avoided by using '\n'
  rather than "\n".

- If the stream buffer uses a buffer, the buffer limits are checked
  twice rather than only once (for most cases; of course, if the buffer
  is not of sufficient size, the bounds may still be checked more than
  once).

My guess is that there is a notable difference between putting things
into one string and using a separate '\n'. Whether this matters is, of
course, a complete different things. Personally, I'm using '\n' or,
better, include the character in a string if there is such a string.

> Or, alternately, is it that hard for a compiler to optimize?

I doubt that any compiler will optimize this stuff: It is distributed
over multiple function calls which would have to be lumped together. It
is unlikely that a compiler will be optimized for this specific
situation. On the other hand, there is also no way to do it in the
library.

> Personally, I got very used to 'endl' before reading Scott Meyer's
> article; I went to 'nl' because I'm used to that kind of thing, find
> it fast to type and easy to read. (My code is all numerical, the I/O
> is something like .001% of the run time. ;-)

This is, however, not the case in all applications. In this situation
you don't really have to care about this stuff but for other
applications there may be a problem...
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk