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" &l开发者_如何学编程t;< 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
.
精彩评论