加速控制台时Setvbuf()问题 [英] Setvbuf() problem when speeding console

查看:88
本文介绍了加速控制台时Setvbuf()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Windows中调高控制台输出,建议使用setvbuf()。

我的问题是,在创建缓冲区之后,cout在被调用的函数中不起作用。



我必须在以下程序中插入main()行:

In order to spped the console output in windows it is recommended to use setvbuf().
My problem is that after making the buffer, cout does not work in called functions.

I had to insert at the following program at main() the line:

cout<<"=== FIN ==="<<endl;





所以:

1.如何强制在函数内写入cout?

2.如果写入大小大于缓冲区大小会发生什么?



我尝试过:





So:
1. How force to write at cout within the function?
2. What happens if writting size is bigger than the buffer size?

What I have tried:

<pre>#include <iostream>
using namespace std;
void test1(){cout<<" function ini"<<endl;} //" function ini" is not written here

int 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); 
#endif

	cout<<"=== INI ==="<<endl;
	test1();
	cout<<"=== FIN ==="<<endl;
}





注意:我也尝试将buffer_setvbuf []设置为具有相同结果的全局变量。



Note: I tried also set buffer_setvbuf[] as a global variable with same results.

推荐答案

在达到缓冲区的长度(或刷新输出)之前不会显示输出,请参阅此处的答案: C:为什么fprintf(stdout,....)这么慢? - 堆栈溢出 [ ^ ]。



output is not displayed until the length of the buffer is reached (or output is flushed), see the answer here: C: Why is a fprintf(stdout,....) so slow? - Stack Overflow[^].

Quote:

1。如何在函数内强制写入cout?

1. How force to write at cout within the function?



我会维护行缓冲策略。另一方面,如果你坚持使用完整的缓冲区,那么你必须明确地刷新 cout






I would maintain the line buffer policy. On the other hand, if you insist on using the full buffer one, then you have to explicitely flush cout.


Quote:

2。如果写入大小大于缓冲区大小会发生什么?

2. What happens if writting size is bigger than the buffer size?

精彩:在达到缓冲区大小时,缓冲区内容被发送到屏幕,然后整个缓冲区可用于进一步输出。

The wonderful: on reaching buffer size, the buffer content is sent to the screen and the whole buffer is then available for further output.

请参阅缓冲效果 [ ^ ]


这篇关于加速控制台时Setvbuf()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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