Most efficient way to output a newline

You don't specify whether you are demanding that the update to the screen is immediate or deferred until the next flush. Therefore:

if you're using iostream io:

cout.put('\n');

if you're using stdio io:

std::putchar('\n');

On Ubuntu 15.10, g++ v5.2.1 (and an older vxWorks, and OSE)

It is easy to demonstrate that

std::cout << std::endl;

puts a new line char into the output buffer, and then flushes the buffer to the device.

But

std::cout << "\n";

puts a new line char into the output buffer, and does not output to the device. Some future action will be needed to trigger the output of the newline char in the buffer to the device.

Two such actions are:

std::cout << std::flush;  // will output the buffer'd new line char

std::cout << std::endl;   // will output 2 new line chars

There are also several other actions that can trigger the flush of the std::cout buffering.


#include <unistd.h>  // for Linux
void msDelay (int ms) { usleep(ms * 1000); }

int main(int, char**)
{
   std::cout << "with endl and no delay " << std::endl;

   std::cout << "with newline and 3 sec delay " << std::flush << "\n";
   msDelay(3000);

   std::cout << std::endl << " 2 newlines";
   return(0);
}

And, per comment by someone who knows (sorry, I don't know how to copy his name here), there are exceptions for some environments.


The answer to this question is really "it depends".

In isolation - if all you're measuring is the performance of writing a '\n' character to the standard output device, not tweaking the device, not changing what buffering occurs - then it will be hard to beat options like

 putchar('\n');
 fputchar('\n', stdout);

 std::cout.put('\n');

The problem is that this doesn't achieve much - all it does (assuming the output is to a screen or visible application window) is move the cursor down the screen, and move previous output up. Not exactly a entertaining or otherwise valuable experience for a user of your program. So you won't do this in isolation.

But what comes into play to affect performance (however you measure that) if we don't output newlines in isolation? Let's see;

  • Output of stdout (or std::cout) is buffered by default. For the output to be visible, options include turning off buffering or for the code to periodically flush the buffer. It is also possible to use stderr (or std::cerr) since that is not buffered by default - assuming stderr is also directed to the console, and output to it has the same performance characteristics as stdout.
  • stdout and std::cout are formally synchronised by default (e.g. look up std::ios_base::sync_with_stdio) to allow mixing of output to stdout and std::cout (same goes for stderr and std::cerr)
  • If your code outputs more than a set of newline characters, there is the processing (accessing or reading data that the output is based on, by whatever means) to produce those other outputs, the handling of those by output functions, etc.
  • There are different measures of performance, and therefore different means of improving efficiency based on each one. For example, there might be CPU cycles, total time for output to appear on the console, memory usage, etc etc
  • The console might be a physical screen, it might be a window created by the application (e.g. hosted in X, windows). Performance will be affected by choice of hardware, implementation of windowing/GUI subsystems, the operating system, etc etc.

The above is just a selection, but there are numerous factors that determine what might be considered more or less performance.


putchar('\n') is the most simple and probably fastest. cout and printf with string "\n" work with null terminated string and this is slower because you process 2 bytes (0A 00). By the way, carriage return is \r = 13 (0x0D). \n code is Line Feed (LF).

Tags:

C++