Throughput of iostream is considered much slower compared with its C counterpart.
cout << "1234567" << endl;
If you iterate 105 times, printf takes about 50ms while cout takes >1s. This looks quite annoying.
After digging into the source of iostream (not an easy task 🙂 ), I finally figure out that the bottleneck is the implicit flush inside ‘endl’.
‘endl’ does more than outputting a single ‘\n’. The standard (C++03 184.108.40.206 / 5) says:
Effects: Calls os.put(os.widen(’\n’) ), then os.flush().
After eliminating the unnecessary flush, cout is even faster than printf in some cases!
Here is the number (the time below is measured by redirecting the output to ‘nul’):
|cout||“1234567\n”||i << “\n”|
|cout||“1234567” << endl||i << endl|
Note that the standard requires the library not to use full-buffer for stdout / cout (if the destination is a file, then it is on), so you have to do it by yourself. Otherwise the performance will be very poor (10x slower). GCC turns on full-buffer even though this is non-conformant :-).
C99 7.19.3 / 7: As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.