Zoom Timer: How to get the time when I need?

So, I read this enlarge documents , but I still donโ€™t see how to do such a simple thing

int main() { //stuff startTimer(); // do stuff int i =getTimerValue(); //stuff } 

to get the runtime of the things I did. How to do it?

+7
c ++ boost timer
source share
3 answers

Use boost::timer

 #include <boost/timer.hpp> int main() { boost::timer t; // start timing ... double elapsed_time = t.elapsed(); ... } 

Note that destuctor a boost::progress_timer display the time. So use the scope if your goal is to simply show the time elapsed in the middle of the function.

 int main() { { boost::progress_timer t; // start timing ... } // elapsed time displayed here when t is destructed ... } 
+19
source share

Replace it with

 #include <boost/progress.hpp> void function() { progress_timer t; // start timing // do stuff return 0; } 

and you get what you want but donโ€™t use printf .

The timer starts with construction and is displayed upon destruction (i.e., at the output of fn). This is usually an RAII way to perform tasks with scope (synchronization, locking, etc.) in C ++.

+5
source share

There is also the following idea, based on the mentioned function, that the elapsed time is shown by the 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; // ... } 
0
source share

All Articles