【发布时间】:2018-06-11 10:08:32
【问题描述】:
Windows cout 和 printf 真的很慢,所以当发送大量数据时,它会降低应用程序的速度(它会发生在代码运行几天以检查是否一切正常时)。 加快速度的一种方法是在 main() 函数的开头编写以下代码来使用缓冲区:
#ifndef __linux__ //Introduce this code at the beginning of main() to increase a lot the speed of cout in windows:
char buffer_setvbuf[1024];setvbuf(stdout, buffer_setvbuf, _IOFBF, sizeof buffer_setvbuf); //¿¡¡Sometimes it does not print cout inside a function until cout is used in main() or end of buffer is reached.
#endif
但不幸的是,有时它不会打印数据,因为缓冲区未满。
然后是问题: 1.如何强制打印:通过制作\n? 2. 如何禁用缓冲区?
【问题讨论】:
-
我不明白问题的第 2 部分。您是否尝试设置缓冲区?
-
如果你需要在缓冲区未满的情况下打印数据,我相信
std::flush是你所需要的。 -
MSalters:设置缓冲区后,可以禁用吗? Croolman:谢谢,我赞成你的回答
标签: c++