Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost timer: how to get time when I need?

Tags:

c++

timer

boost

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?

like image 523
Rella Avatar asked Nov 05 '10 11:11

Rella


2 Answers

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
  ...
}
like image 172
log0 Avatar answered Sep 20 '22 23:09

log0


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++.

like image 25
Steve Townsend Avatar answered Sep 21 '22 23:09

Steve Townsend