Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.45 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Timer.hpp
  2.  
  3.  
  4. #ifndef _VR2_TIMER_HPP
  5. #define _VR2_TIMER_HPP
  6.  
  7.  
  8. #ifndef WIN32
  9.  
  10. #include <sys/time.h>
  11. #include <time.h>
  12. #include <stdint.h>
  13. namespace utils
  14. {
  15.         class Timer
  16.         {
  17.  
  18.                 timeval m_tic, m_toc;
  19.  
  20.                 public:
  21.                 bool isValid();
  22.                 void tic();
  23.                 double toc();
  24.         };
  25. #else
  26. #include <iostream>
  27. #include <windows.h>
  28. namespace utils
  29. {
  30.         class Timer
  31.         {
  32.                 static LARGE_INTEGER m_timerResolution;
  33.                 static bool m_valid;
  34.                 LARGE_INTEGER m_tic, m_toc;
  35.                 public:
  36.  
  37.                 Timer()
  38.                 {
  39.                         //std::cerr << "Timer resolution: " << m_timerResolution.QuadPart << "Hz." << std::endl;
  40.                 }
  41.                 bool isValid();
  42.                 void tic();
  43.                 double toc();
  44.         };
  45. #endif
  46. }
  47. #endif
  48.  
  49.  
  50. // Timer.cpp
  51.  
  52.  
  53. #include "Timer.hpp"
  54.  
  55. namespace utils
  56. {
  57.  
  58. #ifndef WIN32
  59.  
  60.         bool Timer::isValid()
  61.         {
  62.                 return true;
  63.         }
  64.  
  65.         void Timer::tic()
  66.         {
  67.                 gettimeofday(&m_tic, NULL);
  68.         }
  69.  
  70.         double Timer::toc()
  71.         {
  72.                 gettimeofday(&m_toc, NULL);
  73.  
  74.                 uint64_t us = ((m_toc.tv_sec * 1000000) + m_toc.tv_usec) -
  75.                         ((m_tic.tv_sec * 1000000) + m_tic.tv_usec);
  76.  
  77.                 return (double)us/1000000.0;
  78.         }
  79.  
  80. #else
  81.  
  82.         LARGE_INTEGER Timer::m_timerResolution;
  83.         bool Timer::m_valid = (0 != QueryPerformanceFrequency(&m_timerResolution));
  84.  
  85.         bool Timer::isValid()
  86.         {
  87.                 return m_valid;
  88.         }
  89.  
  90.         void Timer::tic()
  91.         {
  92.                 QueryPerformanceCounter(&m_tic);
  93.         }
  94.  
  95.         double Timer::toc()
  96.         {
  97.                 QueryPerformanceCounter(&m_toc);
  98.                 return (m_toc.QuadPart - m_tic.QuadPart)/(double)m_timerResolution.QuadPart;
  99.         }
  100.  
  101. #endif
  102. }