简体   繁体   中英

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 << "Started: "<< before;

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.

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.

To get cout to print sooner, use

std::cout << std::flush;

or use std::endl , which is equivalent to "\\n" followed by std::flush .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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