How to print a new line without clearing the buffer?

I experimented with C ++ trying to figure out how I can print numbers from 0 to n as quickly 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 clears the buffer after every single number that it issues, and of course it should take some time, so I tried to print all the numbers in the buffer first (or actually, until it is full as it seems to automatically collapse), and then immediately clear everything. However, it seems that printing \ n after flushing a buffer like std :: endl, so I omitted it:

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

It seems to work about 10 times faster than the first example. However, I want to know how to store all values ​​in a buffer and flush it all at once, rather than letting it flush every time it is populated, so I have a few questions:

  • Is it possible to print a new line without flushing the buffer?
  • How to resize a buffer so that I can store all the values ​​inside it and clear it at the very end?
  • Is this text output method stupid? If so, why, and what would be the best alternative to this?

EDIT: It seems that my results were biased regarding the lagging system (smartphone terminal application) ... With a faster system, the execution time does not show a significant difference.

+6
source share
1 answer

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

Explanation: std::endl causes a buffer std::endl , while '\n' does not. However, you may or may not notice any acceleration depending on the testing method 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 them compiled using g++ on my Linux system and pass the following tests:

1. time ./a.out

Endl.cpp requires 19.415s.
Slashn.cpp requires 19.312 s.

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

Endl.cpp requires 0.397s
0.153s is required for slashn.cpp

3. time ./a.out >temp

Endl.cpp requires 2.255s
Slashn.cpp requires 0.165s

Conclusion: '\n' definitely faster (even practically), but the difference in speed may depend on other factors. In the case of the terminal window, the limiting factor seems to depend on how quickly the terminal itself can display text. Since the text is displayed on the screen, as well as automatic scrolling, etc., a significant slowdown occurs during execution. On the other hand, for regular files (for example, the temp example above), the speed with which the buffer is reset greatly affects it. In the case of some special files (e.g. /dev/null above), since the data is just sunk into a black hole, flushing seems to have no effect.

+3
source

Source: https://habr.com/ru/post/989483/


All Articles