/src/Utilities/Timer.h

https://github.com/lolow/oboe-fix · C Header · 74 lines · 47 code · 13 blank · 14 comment · 0 complexity · 36ad32508cf514c0224adc5a9e3b0c3c MD5 · raw file

  1. // Copyright (c) 2004-2007 University of Geneva, HEC, Logilab
  2. //
  3. // OBOE is published under the Common Public License.
  4. //
  5. // Authors :
  6. // The OBOE team
  7. //
  8. #ifndef TIMER_H
  9. #define TIMER_H
  10. #include <config.h>
  11. #include <iostream>
  12. #if (defined(LINUX))
  13. #include <sys/times.h>
  14. #include <unistd.h>
  15. #endif
  16. #if (defined(WIN32))
  17. #include <time.h>
  18. //#include <unistd.h>
  19. #endif
  20. /**
  21. * The Timer class.
  22. * Takes care of tracking Real, CPU and System time.
  23. **/
  24. namespace Accpm {
  25. class Timer {
  26. #if (defined(LINUX))
  27. private:
  28. struct tms _startTime;
  29. clock_t _startClock;
  30. struct tms _endTime;
  31. clock_t _endClock;
  32. const double _clockTicks;
  33. public:
  34. Timer() : _clockTicks(sysconf(_SC_CLK_TCK)) {}
  35. inline void start() { _startClock = times(&_startTime); }
  36. inline void stop() { _endClock = times(&_endTime); }
  37. double getRealTime() const { return (_endClock - _startClock)/_clockTicks;}
  38. double getCpuTime() const { return (_endTime.tms_utime - _startTime.tms_utime
  39. + _endTime.tms_stime - _startTime.tms_stime)/_clockTicks; }
  40. double getSysTime() const { return (_endTime.tms_stime - _startTime.tms_stime)/_clockTicks; }
  41. #endif
  42. #if (defined(WIN32))
  43. private:
  44. time_t _startTime;
  45. clock_t _startClock;
  46. time_t _endTime;
  47. clock_t _endClock;
  48. const double _clockTicks;
  49. public:
  50. Timer() : _clockTicks(CLK_TCK) {}
  51. inline void start() { _startClock = time(&_startTime); _startClock = clock_t();}
  52. inline void stop() { _endClock = time(&_endTime); _endClock = clock_t();}
  53. double getRealTime() const { return _endTime - _startTime;}
  54. //double getCpuTime() const { return (_endClock - _startClock)/_clockTicks; }
  55. // Dont know how to get CPU time on windows
  56. double getCpuTime() const { return getRealTime(); }
  57. double getSysTime() const { std::cout << "Function Timer::getSysTime() not supported\n"; return 0; }
  58. #endif
  59. };
  60. }
  61. #endif