#ifndef TIMER_H
#define TIMER_H 1

#include <iostream>

extern "C"
{
#include <sys/time.h>
}

using namespace std;

class Timer
{
  struct timeval timerStart, timerStop;
  public:

  void start()
  {
    if(0 != gettimeofday(&timerStart, 0))
      perror("timer.start(): gettimeofday() failed");
  }

  void stop()
  {
    if(0 != gettimeofday(&timerStop, 0))
      perror("timer.stop(): gettimeofday() failed");
  }

  /**
    * returns time between start() and stop() in microseconds
    */
  long get()
  {
    int dseconds = timerStop.tv_sec-timerStart.tv_sec;
    long diff = 1000000*(dseconds) + timerStop.tv_usec - timerStart.tv_usec;
    if(diff<0) cerr << "start: " <<timerStart.tv_sec << " " << timerStart.tv_usec
      << " stop: " << timerStop.tv_sec << " " << timerStop.tv_usec << endl;
    return diff;
  }
};

#endif

