13

Possible Duplicate:
C++: “std::endl” vs “\n”

In Accelerated C++, two things are mentioned:

  1. Most systems take a significant amount of time to write characters to an output device. Because of this, C++ accumulates characters to be written in a buffer and waits for the buffer to be flushed.

  2. One way a buffer can be flushed is if we explicitly tell it to do so by using std::endl.

This made me wonder: Obviously the benefits would be very small and unnoticeable for everything except the largest of outputs, but is using "\n" faster than using std::endl, or does "\n" also flush a buffer?

Community
  • 1
  • 1
  • 1
    Yes, this is an implementation detail, so you won't get a definite answer. My guess is that it makes a difference in most implementations, but this does not have to be true. – Niklas B. Mar 11 '12 at 00:10
  • @Carl -- Admittedly, I did not, I wouldn't even know how to. I was reading the book and I was curious, so I thought I'd ask. –  Mar 11 '12 at 00:10

2 Answers2

11

Using '\n' does not flush the buffer and is indeed faster than using std::endl.

In typical I/O, output is buffered before it's written to the intended device. This way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character. Flushing "flushes" the buffer onto the device, causing a definite performance overhead.

-Adapted from: C++ - endl and flushing the buffer

Community
  • 1
  • 1
Shantanu
  • 373
  • 2
  • 8
  • 1
    For some [generally] very small value of "faster" ;-) –  Mar 11 '12 at 00:23
  • 2
    @pst: well, when outputting large quantity of data to file it may actually kill the performance (it happened to me once when writing some tens of MBs of data in CSV). – Matteo Italia Mar 11 '12 at 00:37
  • @MatteoItalia True, I was not thinking of the inverse :( –  Mar 11 '12 at 00:40
  • @MatteoItalia: On other hand iostream isn't the fastest way to output something, so there will many other things that'll impact performance negatively. – SigTerm Mar 11 '12 at 00:41
  • 3
    @SigTerm: iostreams has a horrible design, but in the `g++` implementation in several occasions I found out that it was slightly faster than C stdio. – Matteo Italia Mar 11 '12 at 00:48
0

I'd like to add that I think meaning of writing '\n' to the stream could be different from writing std::endl/std::flush in third-party libraries.

For example, I'm using ostream-based logger in current project. That logger uses std::stringstream functionality for output formatting, but has overridden manipulators for flushing. This allows to write '\n' to the log without flushing and simplifies code.

Here is a pseudocode sample:

class MyStream
{
    // [cut]
    std::stringstream m_buffer;
    // [cut]
};

// friends:
template <typename Printable>
MyStream& operator<<(MyStream& stream, const Printable& value)
{
     stream.m_buffer << value;
}

typedef decltype(std::flush) TManipulator;
template <>
MyStream& operator<<(MyStream& stream, const TManipulator& manipulator)
{
     if ( manipulator == std::flush || manipulator == std::endl )
         stream.sendLogLine();
     else
         stream.m_buffer << manipulator;
}

// usage sample
void main()
{
    getLoggerStream() << "hello" << std::endl;
}

P.S. I didn't like to subclass std::stringstream, so MyStream is an adapter. If I want to make '\n' flushing I should reimplement much more functionality including char*, std::string and other specializations.

Victor Istomin
  • 1,107
  • 8
  • 14