00001
00002
00003 #ifndef PERFORMANCE_COUNTER_H
00004 #define PERFORMANCE_COUNTER_H
00005
00006 #include <windows.h>
00007 #include <iosfwd>
00008
00012 class PerformanceCounter
00013 {
00014 public:
00018 PerformanceCounter()
00019 {
00020 QueryPerformanceCounter(&m_Counter);
00021 }
00025 PerformanceCounter(LONGLONG ll)
00026 {
00027 m_Counter.QuadPart = ll;
00028 }
00034 PerformanceCounter(long days, long hours, long minutes, long seconds, long milliseconds, long microseconds)
00035 {
00036 m_Counter.QuadPart = (days*3600*24+hours*3600+minutes*60+seconds)*GetFrequency();
00037 m_Counter.QuadPart += milliseconds * GetFrequency() / 1000;
00038 m_Counter.QuadPart += microseconds * GetFrequency() / 1000000;
00039 }
00040 PerformanceCounter operator- (const PerformanceCounter& pc) const
00041 {
00042 return PerformanceCounter(m_Counter.QuadPart - pc.m_Counter.QuadPart);
00043 }
00044 PerformanceCounter operator+ (const PerformanceCounter& pc) const
00045 {
00046 return PerformanceCounter(m_Counter.QuadPart + pc.m_Counter.QuadPart);
00047 }
00048 PerformanceCounter& operator+= (const PerformanceCounter& pc)
00049 {
00050 m_Counter.QuadPart += pc.m_Counter.QuadPart;
00051 return *this;
00052 }
00053 PerformanceCounter& operator-= (const PerformanceCounter& pc)
00054 {
00055 m_Counter.QuadPart -= pc.m_Counter.QuadPart;
00056 return *this;
00057 }
00058 bool operator== (const PerformanceCounter& pc) const
00059 {
00060 return m_Counter.QuadPart == pc.m_Counter.QuadPart;
00061 }
00062 bool operator!= (const PerformanceCounter& pc) const
00063 {
00064 return m_Counter.QuadPart != pc.m_Counter.QuadPart;
00065 }
00066 bool operator< (const PerformanceCounter& pc) const
00067 {
00068 return m_Counter.QuadPart < pc.m_Counter.QuadPart;
00069 }
00070 bool operator> (const PerformanceCounter& pc) const
00071 {
00072 return m_Counter.QuadPart > pc.m_Counter.QuadPart;
00073 }
00074 bool operator<= (const PerformanceCounter& pc) const
00075 {
00076 return m_Counter.QuadPart <= pc.m_Counter.QuadPart;
00077 }
00078 bool operator>= (const PerformanceCounter& pc) const
00079 {
00080 return m_Counter.QuadPart >= pc.m_Counter.QuadPart;
00081 }
00086 void GetAsSecondsAndMicroseconds(long& seconds, long& microseconds)
00087 {
00088 seconds = (long)(m_Counter.QuadPart / GetFrequency());
00089 microseconds = (long)((m_Counter.QuadPart - seconds*GetFrequency())*1000000/GetFrequency());
00090 }
00094 double GetAsMilliseconds()
00095 {
00096 return m_Counter.QuadPart * 1000.0 / GetFrequency();
00097 }
00101 static void Init();
00105 static LONGLONG GetFrequency() { return m_llFrequency; }
00109 static bool IsInit() { return m_llFrequency != 0; }
00110 protected:
00111 LARGE_INTEGER m_Counter;
00112 static LONGLONG m_llFrequency;
00113
00114 friend std::ostream& operator << (std::ostream& os, const PerformanceCounter& );
00115 friend std::ostream& operator<< (std::ostream&, const class format_performance_counter &);
00116 };
00117
00130 class format_performance_counter
00131 {
00132 public:
00133 enum { show_seconds=1, show_milliseconds=2, show_microseconds=4, show_days=8};
00134 format_performance_counter(PerformanceCounter pc, int options=show_seconds | show_milliseconds | show_microseconds)
00135 : m_pc(pc), m_options(options)
00136 {};
00137
00142 static int width(int options=show_seconds | show_milliseconds | show_microseconds);
00143
00144 PerformanceCounter m_pc;
00145 int m_options;
00146
00147 friend std::ostream& operator<< (std::ostream&, const format_performance_counter &);
00148 };
00149
00150 #endif // PERFORMANCE_COUNTER_H