It depends on your OS an your platform's implementation of the C and C++ I/O libraries (... and cpu load, services, processes, RAM...).
On Windows, writing to the console is a huge bottleneck. Usually it's faster on Linux / MacOS (e.g. Performance difference of iostream console output between Windows and OSX?).
Writing to an ofstream
directly could increase performance if it uses a different buffering scheme compared to cout
(and this is often the case).
Anyway with streams
you can speed up the printing significantly using '\n'
instead of std::endl
:
std::cout << "Test line\n";
is faster than:
std::cout << "Test line" << std::endl;
since the latter is equivalent to:
std::cout << "Test line\n" << std::flush;
(see C++: "std::endl" vs "\n" for further details).
Some references: