Possible Duplicate:
C++: “std::endl” vs “\n”
I have a simple program I tested and I realise that endl
wreaks havoc on my program. Using endl, my program ran in 100+ ms while working with '\n'
, the time dropped to ~50ms.
Can anyone tell why is there such a difference?
P.S. I did read other posts that somehow explained what each of them are doing, but does std::flush
really take so much time?
Or could there be another possible explanation?
std::endl
writes a newline, and flushes the buffer. As you have discovered, the flush can be a rather expensive operation.
endl
has extra expensive flush()
operation
27.7.3.8 Standard basic_ostream manipulators [ostream.manip]
namespace std {
template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
}
1 Effects: Calls os.put(os.widen(’\n’)), then os.flush().
2 Returns: os.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With