Duration.h

Go to the documentation of this file.
00001 // Duration.h: interface for the Duration class.
00002 //
00004 
00005 #if !defined(AFX_DURATION_H__95A8A4BA_3CFB_4ABC_8C48_495328EA1336__INCLUDED_)
00006 #define AFX_DURATION_H__95A8A4BA_3CFB_4ABC_8C48_495328EA1336__INCLUDED_
00007 
00008 #if _MSC_VER > 1000
00009 #pragma once
00010 #endif // _MSC_VER > 1000
00011 
00012 #ifdef WIN32
00013         #pragma once
00014         #include <windows.h>
00015 #else
00016         #include <sys/time.h>
00017         #define TIMEVAL struct timeval 
00018         #define TIMEVAL struct timeval 
00019         
00020         #ifndef NULL
00021                 #define NULL 0
00022         #endif
00023 
00024         #define GET_TIME(T) gettimeofday(&T,NULL)
00025         #define ELAPSED_TIME_SEC(B, A) \
00026                 static_cast<double>((A.tv_sec - B.tv_sec) + (1.E-6 * (A.tv_usec - B.tv_usec))) // B = start; A = end #endif
00027 #endif
00028 
00029 class Duration  
00030 {
00031 public:
00032         Duration();
00033         virtual ~Duration() {};
00034 
00035         void Start(void);
00036         void Stop(void);
00037         double GetDuration(void) const;
00038         double GetDurationInMs(void) const;
00039 
00040 protected:
00041 
00042 #ifdef WIN32
00043         LARGE_INTEGER m_liStart;
00044         LARGE_INTEGER m_liStop;
00045 
00046         LONGLONG m_llFrequency;
00047         LONGLONG m_llCorrection;
00048 #else
00049         TIMEVAL         m_liStart;
00050         TIMEVAL         m_liStop;
00051 //      LONGLONG        m_llCorrection;
00052 #endif
00053 };
00054 
00055 
00056 inline Duration::Duration(void)
00057 {
00058 #ifdef WIN32
00059         LARGE_INTEGER liFrequency;
00060 
00061         QueryPerformanceFrequency(&liFrequency);
00062         m_llFrequency = liFrequency.QuadPart;
00063         
00064         // Calibration
00065         Start();
00066         Stop();
00067 
00068         m_llCorrection = m_liStop.QuadPart-m_liStart.QuadPart;
00069 #else
00070         // Calibration
00071 //      Start();
00072 //      Stop();
00073 
00074 //      m_llCorrection = GetDuration();
00075 #endif
00076 }
00077 
00078 inline void Duration::Start(void)
00079 {
00080 
00081 #ifdef WIN32
00082         // Ensure we will not be interrupted by any other thread for a while
00083         Sleep(0);
00084         QueryPerformanceCounter(&m_liStart);
00085 #else
00086         GET_TIME(m_liStart);
00087 #endif
00088 
00089 
00090 }
00091 
00092 inline void Duration::Stop(void)
00093 {
00094 #ifdef WIN32
00095         QueryPerformanceCounter(&m_liStop);
00096 #else
00097         GET_TIME(m_liStop);
00098 #endif
00099 
00100 }
00101 
00102 inline double Duration::GetDuration(void) const
00103 {
00104 #ifdef WIN32
00105         return (double)(m_liStop.QuadPart-m_liStart.QuadPart-m_llCorrection)*1000000.0 / m_llFrequency; 
00106 #else
00107         return ELAPSED_TIME_SEC(m_liStart, m_liStop);
00108 #endif
00109 }
00110 
00111 inline double Duration::GetDurationInMs(void) const
00112 {
00113 #ifdef WIN32
00114         return (double)(m_liStop.QuadPart-m_liStart.QuadPart-m_llCorrection)*1000000.0 / m_llFrequency /1000.0; 
00115 #else
00116         return ELAPSED_TIME_SEC(m_liStart, m_liStop)*1000.0;  
00117 #endif
00118 }
00119 
00120 
00121 #endif // !defined(AFX_DURATION_H__95A8A4BA_3CFB_4ABC_8C48_495328EA1336__INCLUDED_)

Generated on Fri Jul 10 10:31:53 2009 for Utils Library by  doxygen 1.4.5