Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c++ - How can I print a newline without flushing the buffer?

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 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Explanation: std::endl causes a flushing of the buffer, whereas ' ' 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 << '
';
    }
}

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: ' ' 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...