Boost logo

Boost Users :

Subject: Re: [Boost-users] boost timer
From: Chris Cleeland (chris.cleeland_at_[hidden])
Date: 2011-08-15 09:54:09


If you are on an x86 platform, you can use the "rdtsc" assembly
instruction to obtain processor tick counts.

When I needed accurate timing, I created a function to wrap around
"rdtsc", a Timer class with an API something like

class Timer
{
public:
   void start();
   void stop();
   ulonglong elapsed();
};

Then created class using the Guard idiom around that:

class AutoTimer
{
public:
   AutoTimer(Timer& t) : t_(t) { t_.start(); }
   ~AutoTimer() { t_.stop(); }
private:
  Timer& t_;
};

Becomes very easy to time segments of code:

...
  Timer t1;
...
  {
    AutoTimer a(t1);
...do stuff...
  }

One can also continue to start/stop a Timer, thus accumulating total
tick counts.

Conversion of tick counts back to real time is done external to any of
these classes.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net