如何在不刷新缓冲区的情况下打印换行符? [英] How can I print a newline without flushing the buffer?

查看:49
本文介绍了如何在不刷新缓冲区的情况下打印换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c ++尝试弄清楚如何尽可能快地将数字从0打印到n.

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;
} 

但是,我认为这会在它输出的每个数字之后刷新缓冲区,并且肯定要花一些时间,所以我尝试先将所有数字打印到缓冲区(或者实际上一直打印到缓冲区满为止,然后看起来(自动刷新),然后立即将其全部刷新.但是似乎在刷新缓冲区后打印\ n就像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;

这似乎比第一个示例快10倍.但是,我想知道如何将所有值存储在缓冲区中并一次刷新它们,而不是让它在每次变满时刷新它,所以我有几个问题:

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. 是否可以在不刷新缓冲区的情况下打印换行符?
  2. 如何更改缓冲区大小,以便可以将所有值存储在其中并在最后将其刷新?
  3. 这种输出文本哑音的方法吗?如果是这样,为什么,还有什么更好的替代方法?

编辑:似乎我的结果受到系统落后(智能手机的终端应用程序)的偏见...系统速度越快,执行时间就没有显着差异.

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.

推荐答案

TL; DR:通常,使用'\ n'代替 std::endl 更快,因为 std :: endl

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

说明: std :: endl 导致刷新缓冲区,而'\ n'则不会.但是,根据您应用的测试方法,您可能会注意到也可能不会注意到任何提速.

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.

请考虑以下测试文件:

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';
    }
}

这两个都是在我的linux系统上使用 g ++ 进行编译的,并经过以下测试:

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

1.时间./a.out

1. time ./a.out

对于endl.cpp,需要19.415秒.
对于slashn.cpp,它需要19.312秒.

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

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

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

对于endl.cpp,需要0.397s
对于slashn.cpp,需要0.153s

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

3.时间./a.out> temp

3. time ./a.out >temp

对于endl.cpp,需要2.255秒
对于slashn.cpp,需要0.165s

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

结论: '\ n'肯定更快(甚至实际上),但是速度差异可能取决于其他因素.在终端窗口的情况下,限制因素似乎取决于终端本身可以显示文本的速度.当文本显示在屏幕上,并且需要进行自动滚动等操作时,执行速度会大大降低.另一方面,对于普通文件(如上面的 temp 示例),刷新缓冲区的速率对其影响很大.对于某些特殊文件(例如上面的/dev/null ),由于数据只是陷入了黑洞,因此​​刷新似乎没有效果.

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.

这篇关于如何在不刷新缓冲区的情况下打印换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆