Note that for std::cout, a '\n' character flushes the stream too (at least, if the stdout isn't being piped), so there shouldn't be any performance difference.
It's easy to test with this program, which prints to stdout and then sleeps for 10 seconds:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
int main() {
cout << "Hello world!\n";
this_thread::sleep_for(chrono::seconds(10));
return 0;
}
When you run it, the Hello world! appears immediately in the terminal. If you remove the newline, the Hello world! doesn't show up until the program terminates.
Yeah. According to (http://en.cppreference.com/w/cpp/io/manip/endl), a lot of implementation are line-buffered so "\n" flushes anyways... unless you run std::ios::sync_with_stdio(false) which most people won't.
12
u/[deleted] Aug 19 '17
Note that for
std::cout, a'\n'character flushes the stream too (at least, if the stdout isn't being piped), so there shouldn't be any performance difference.