Go to the documentation of this file.00001 #ifndef OPENTISSUE_UTILITY_UTILITY_TIMER_H
00002 #define OPENTISSUE_UTILITY_UTILITY_TIMER_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #ifdef WIN32
00013 # define NOMINMAX
00014 # define WIN32_LEAN_AND_MEAN
00015 # include <windows.h>
00016 # undef WIN32_LEAN_AND_MEAN
00017 # undef NOMINMAX
00018 #else
00019 # include<sys/time.h>
00020 #endif
00021
00022 #include <cassert>
00023
00024 namespace OpenTissue
00025 {
00026 namespace utility
00027 {
00028
00049 template<typename real_type>
00050 class Timer
00051 {
00052 #ifdef WIN32
00053 private:
00054 LARGE_INTEGER m_start;
00055 LARGE_INTEGER m_end;
00056 LARGE_INTEGER m_freq;
00057 bool m_first;
00058 public:
00059 Timer():m_first(true){}
00060 public:
00061 void start()
00062 {
00063 if(m_first)
00064 {
00065 QueryPerformanceFrequency(&m_freq);
00066 m_first = false;
00067 }
00068 QueryPerformanceCounter(&m_start);
00069 }
00070 void stop()
00071 {
00072 QueryPerformanceCounter(&m_end);
00073 }
00074 real_type operator()()const
00075 {
00076 real_type end = static_cast<real_type>(m_end.QuadPart);
00077 real_type start = static_cast<real_type>(m_start.QuadPart);
00078 real_type freq = static_cast<real_type>(m_freq.QuadPart);
00079 return (end - start)/ freq;
00080 }
00081 #else
00082 private:
00083 struct timeval m_start;
00084 struct timeval m_end;
00085 struct timezone m_tz;
00086 public:
00087 void start() { gettimeofday(&m_start, &m_tz); }
00088 void stop() { gettimeofday(&m_end,&m_tz); }
00089 real_type operator()()const
00090 {
00091 real_type t1 = static_cast<real_type>(m_start.tv_sec) + static_cast<real_type>(m_start.tv_usec)/(1000*1000);
00092 real_type t2 = static_cast<real_type>(m_end.tv_sec) + static_cast<real_type>(m_end.tv_usec)/(1000*1000);
00093 return t2-t1;
00094 }
00095 #endif
00096 };
00097
00098 }
00099 }
00100
00101
00102 #endif