Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print a newline without flushing the buffer?

Tags:

c++

I was experimenting with c++ trying to figure out how I could print the numbers from 0 to n as fast as possible.

At first I just printed all the numbers with a loop:

for (int i = 0; i < n; i++) 
{
    std::cout << i << std::endl;
} 

However, I think this flushes the buffer after every single number that it outputs, and surely that must take some time, so I tried to first print all the numbers to the buffer (or actually until it's full as it seems then seems to flush automatically) and then flush it all at once. However it seems that printing a \n after flushes the buffer like the std::endl so I omitted it:

for (int i = 0; i < n; i++) 
{
    std::cout << i << ' ';
} 
std::cout << std::endl;

This seems to run about 10 times faster than the first example. However I want to know how to store all the values in the buffer and flush it all at once rather than letting it flush every time it becomes full so I have a few questions:

  1. Is it possible to print a newline without flushing the buffer?
  2. How can I change the buffer size so that I could store all the values inside it and flush it at the very end?
  3. Is this method of outputting text dumb? If so, why, and what would be a better alternative to it?

EDIT: It seems that my results were biased by a laggy system (Terminal app of a smartphone)... With a faster system the execution times show no significant difference.

like image 261
user3391899 Avatar asked Jun 21 '15 18:06

user3391899


People also ask

Does newline character flush the buffer?

For a terminal file, the output buffer is flushed when it is full, when a new-line character is written, when terminal input is requested, or when a character is written to the terminal using a different stream.

Does STD Endl flush the buffer?

In C++, we can explicitly be flushed to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream.

Does printf flush the buffer?

If you need to see the output, you need to make sure the buffer is flushed. You can do this for an _IOLBF stream by making sure to end each printf format string with a '\n' (new line). Since the stdout stream is line buffered this will cause the buffer to be flushed.

What is print flush true?

Pythons print method as an exclusive attribute namely, flush which allows the user to decide if he wants his output to be buffered or not. The default value of this is False meaning the output will be buffered. If you change it to true, the output will be written as a sequence of characters one after the other.


1 Answers

TL;DR: In general, using '\n' instead of std::endl is faster since std::endl

Explanation: std::endl causes a flushing of the buffer, whereas '\n' does not. However, you might or might not notice any speedup whatsoever depending upon the method of testing that you apply.

Consider the following test files:

endl.cpp:

#include <iostream>

int main() {
    for ( int i = 0 ; i < 1000000 ; i++ ) {
        std::cout << i << std::endl;
    }
}

slashn.cpp:

#include <iostream>

int main() {
    for ( int i = 0 ; i < 1000000 ; i++ ) {
        std::cout << i << '\n';
    }
}

Both of these are compiled using g++ on my linux system and undergo the following tests:

1. time ./a.out

For endl.cpp, it takes 19.415s.
For slashn.cpp, it takes 19.312s.

2. time ./a.out >/dev/null

For endl.cpp, it takes 0.397s
For slashn.cpp, it takes 0.153s

3. time ./a.out >temp

For endl.cpp, it takes 2.255s
For slashn.cpp, it takes 0.165s

Conclusion: '\n' is definitely faster (even practically), but the difference in speed can be dependant upon other factors. In the case of a terminal window, the limiting factor seems to depend upon how fast the terminal itself can display the text. As the text is shown on screen, and auto scrolling etc needs to happen, massive slowdowns occur in the execution. On the other hand, for normal files (like the temp example above), the rate at which the buffer is being flushed affects it a lot. In the case of some special files (like /dev/null above), since the data is just sinked into a black-hole, the flushing doesn't seem to have an effect.

like image 51
Jay Bosamiya Avatar answered Oct 05 '22 03:10

Jay Bosamiya