Boost timer: how to get time when I need?
So I read this boost docs but I still do not see how to do such simple thing
int main() {
//stuff
startTimer();
// do stuff
int i =getTimerValue();
//stuff
}
so to get execution time of stuff that I've done. 开发者_如何学编程How to do such thing?
Use boost::timer
#include <boost/timer.hpp>
int main() {
boost::timer t; // start timing
...
double elapsed_time = t.elapsed();
...
}
Note that the destuctor of a boost::progress_timer
will display the time. So use scope if your aim is just to display time elapsed in the middle of a function.
int main() {
{
boost::progress_timer t; // start timing
...
} // elapsed time displayed here when t is destructed
...
}
Replace this with
#include <boost/progress.hpp>
void function()
{
progress_timer t; // start timing
// do stuff
return 0;
}
and you will get what you want, not using printf
though.
Timer starts on construction and displays on destruction (ie. at fn exit.). This is a typically RAII way of doing scoped tasks (timing, locking etc.) in C++.
There is also the following idea based on the mentioned feature that the elapsed time is showed by destructor.
#include "boost/timer/timer.hpp"
int main()
{
// ...
boost::timer:auto_cpu_timer *boost_timer = new boost::timer:auto_cpu_timer();
// we want to measure the time of execution of this part of code
// ...
delete boost_timer; // show the elapsed time
// then we can repeat
boost_timer = new boost::timer:auto_cpu_timer();
// ...
delete boost_timer;
// ...
}
精彩评论