[prev in list] [next in list] [prev in thread] [next in thread] 

List:       hpux-cxx-dev
Subject:    using iostreams efficiently (was Re: CXX-DEV: aCC 6.06 patch)
From:       Martin Sebor <sebor () roguewave ! com>
Date:       2005-12-09 16:51:59
Message-ID: 4399B62F.6040103 () roguewave ! com
[Download RAW message or body]

S G Ganesh wrote:
[...]
> 3) Avoiding endl and using '\n' instead
> Endl flushes the output. This might be costly if the endl is 
> used often, since flush() might result in calling write().  

Yes, this is an important guideline, especially since many textbooks
and example programs (unfortunately, including some of our own) tend
to highlight std::endl as "the C++ way" of appending a newline to
output.

std::endl is almost always unnecessary.

Another good advice to keep in mind when it comes to iostreams is to
feel free to drop down to the streambuf layer instead of using the
stream inserters and extractors repeatedly to implement a complex
user-defined inserter or extractor.

Every insertion or extraction has a hidden performance cost due to
the creation and destruction of the sentry object. The sentry must
flush any tied streams and might also lock the stream object in
order to guarantee atomicity.

User-defined inserters and extractors can be avoid much of this
cost by creating their own sentry and using the streambuf object
instead. Here's an example of operator<<() that shows how it can
be done:

http://svn.apache.org/repos/asf/incubator/stdcxx/trunk/examples/manual/list.cpp

Martin
 _________________________________________________________________
 To leave this mailing list, send mail to majordomo@cxx.cup.hp.com
    with the message UNSUBSCRIBE cxx-dev
 _________________________________________________________________
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic