Reputation: 1621
I was wondering what is the most efficient performant way to output a new line to console. Please explain why one technique is more efficient. Efficient in terms of performance.
For example:
cout << endl;
cout << "\n";
puts("");
printf("\n");
The motivation for this question is that I find my self writing loops with outputs and I need to output a new line after all iterations of the loop. I'm trying to find out what's the most efficient way to do this assuming nothing else matters. This assumption that nothing else matters is probably wrong.
Upvotes: 12
Views: 21772
Reputation: 9
Well if you want to change the line I'd like to add the simplest and the most common way which is using (endl), which has the added perk of flushing the stream, unlike cout << '\n';
on its own.
Example:
cout << "So i want a new line" << endl;
cout << "Here is your new line";
Output:
So i want a new line
Here is your new line
This can be done for as much new lines you want. Allow me to show an example using 2 new lines, it'll definitely clear all of your doubts,
Example:
cout << "This is the first line" << endl;
cout << "This is the second line" << endl;
cout << "This is the third line";
Output:
This is the first line
This is the second line
This is the third line
The last line will just have a semicolon to close since no newline is needed. (endl) is also chain-able if needed, as an example, cout << endl << endl;
would be a valid sequence.
Upvotes: 0
Reputation: 36617
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;
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
)The above is just a selection, but there are numerous factors that determine what might be considered more or less performance.
Upvotes: 4
Reputation: 30489
I would suggest to use:
std::cout << '\n'; /* Use std::ios_base::sync_with_stdio(false) if applicable */
or
fputc('\n', stdout);
And turn the optimization on and let the compiler decide what is best way to do this trivial job.
Upvotes: 2
Reputation: 69912
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');
Upvotes: 6
Reputation: 1
It's actually OS/Compiler implementation dependent.
The most efficient, least side effect guaranteed way to output a '\n'
newline character is to use std::ostream::write()
(and for some systems requires std::ostream
was opened in std::ios_base::binary
mode):
static const char newline = '\n';
std::cout.write(&newline,sizeof(newline));
Upvotes: 4
Reputation: 5576
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.
Upvotes: 5
Reputation: 6572
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).
Upvotes: 12