简体   繁体   English

cout的奇怪行为<< Boost :: posix_time

[英]strange behaviour of cout << Boost::posix_time

I want to calculate time of a loop that it takes to finish. 我想计算完成循环所需的时间。

I do something like: 我做的事情如下:

ptime before = second_clock::local_time(); //get current time
cout << "Started: "<< before << " ... processing ...";
while(foo) {
....
}
ptime after = second_clock::local_time(); // get Current time
cout << "Processing took" << after - before;

This will output: Started: "some time" 这将输出:开始:“一段时间”

then I wait for the loop to finish before I get to see " ... processing ..." 然后我等待循环结束才能看到“......处理......”

Why is that? 这是为什么? It should first cout the whole text and then go into the loop. 它应首先播放整个文本,然后进入循环。

If I change the first cout to: 如果我将第一个cout更改为:

cout << "Started: "<< before; cout <<“开始:”<<之前;

It doesn't even show me the time before the loop finishes. 它甚至没有显示循环结束前的时间。

That's the most weird thing I've ever seen...seems there's something going wrong with my understanding of boost time. 这是我见过的最奇怪的事情......似乎我对提升时间的理解出了问题。

I'm using boost::threads too in my code but workers are spwaned within the loop so I don't see how this could have to do with this problem. 我在我的代码中也使用了boost :: threads,但是循环中的工作程序被spwaned所以我不知道这可能与这个问题有什么关系。

Can someone help me out here? 有人可以帮帮我吗?

ostream s including cout use buffers, which means the implementation can wait to see if you will send more data before actually acting on it. ostream包括cout使用缓冲区,这意味着实现可以等到你是否会在实际操作之前发送更多数据。

To get cout to print sooner, use 为了让cout更快打印,请使用

std::cout << std::flush;

or use std::endl , which is equivalent to "\\n" followed by std::flush . 或使用std::endl ,相当于"\\n"后跟std::flush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM