00001 // PerformanceCounter.cpp 00002 00003 #include "PerformanceCounter.h" 00004 #include <sstream> 00005 #include <iomanip> 00006 00007 LONGLONG PerformanceCounter::m_llFrequency = 0; 00008 00009 void PerformanceCounter::Init() 00010 { 00011 LARGE_INTEGER li; 00012 QueryPerformanceFrequency(&li); 00013 m_llFrequency = li.QuadPart; 00014 } 00015 00016 std::ostream& operator<<(std::ostream& os, const PerformanceCounter& pc) 00017 { 00018 os << format_performance_counter(pc); 00019 return os; 00020 } 00021 00022 std::ostream& operator<< (std::ostream &os, const format_performance_counter& fpc) 00023 { 00024 if (!PerformanceCounter::IsInit()) 00025 PerformanceCounter::Init(); 00026 bool showseconds = false; 00027 if (fpc.m_options & format_performance_counter::show_seconds) 00028 showseconds = true; 00029 bool showmilliseconds = false; 00030 if (fpc.m_options & format_performance_counter::show_milliseconds) 00031 { 00032 showmilliseconds = true; 00033 showseconds = true; 00034 } 00035 bool showmicroseconds = false; 00036 if (fpc.m_options & format_performance_counter::show_microseconds) 00037 { 00038 showmicroseconds = true; 00039 showmilliseconds = true; 00040 showseconds = true; 00041 } 00042 bool showdays = false; 00043 if (fpc.m_options & format_performance_counter::show_days) 00044 showdays = true; 00045 00046 LONGLONG ll = fpc.m_pc.m_Counter.QuadPart; 00047 LONGLONG llDays = fpc.m_pc.m_Counter.QuadPart/PerformanceCounter::GetFrequency()/3600/24; 00048 ll -= llDays*3600*24*PerformanceCounter::GetFrequency(); 00049 LONGLONG llHours = ll/PerformanceCounter::GetFrequency()/3600; 00050 ll -= llHours*3600*PerformanceCounter::GetFrequency(); 00051 LONGLONG llMinutes = ll/PerformanceCounter::GetFrequency()/60; 00052 ll -= llMinutes*60*PerformanceCounter::GetFrequency(); 00053 LONGLONG llSeconds = ll / PerformanceCounter::GetFrequency(); 00054 ll -= llSeconds*PerformanceCounter::GetFrequency(); 00055 LONGLONG llMilliSeconds = ll * 1000 / PerformanceCounter::GetFrequency(); 00056 ll -= llMilliSeconds*PerformanceCounter::GetFrequency()/1000; 00057 LONGLONG llMicroSeconds = ll * 1000000 / PerformanceCounter::GetFrequency(); 00058 std::ostringstream ss; 00059 ss << std::setfill('0') << std::fixed << std::setprecision(0); 00060 if (llDays || showdays) 00061 ss << double(llDays) << "+"; 00062 char szTimeSep[4]; 00063 strcpy(szTimeSep, ":\0"); 00064 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, szTimeSep, sizeof(szTimeSep)); 00065 ss << std::setw(2) << double(llHours); 00066 ss << szTimeSep << std::setw(2) << double(llMinutes); 00067 if (showseconds) 00068 ss << szTimeSep << std::setw(2) << double(llSeconds); 00069 if (showmilliseconds) 00070 ss << szTimeSep << std::setw(3) << double(llMilliSeconds); 00071 if (showmicroseconds) 00072 ss << szTimeSep << std::setw(3) << double(llMicroSeconds); 00073 os << ss.str(); 00074 return os; 00075 } 00076 00077 int format_performance_counter::width(int options) 00078 { 00079 int width = 5; 00080 if (options & show_seconds) 00081 width += 3; 00082 if (options & show_milliseconds) 00083 { 00084 width += 4; 00085 if ((options & show_seconds) == 0) 00086 width += 3; 00087 } 00088 if (options & show_microseconds) 00089 { 00090 width += 4; 00091 if ((options & show_seconds) == 0) 00092 width += 3; 00093 if ((options & show_milliseconds) == 0) 00094 width += 4; 00095 } 00096 if (options & show_days) 00097 width += 2; 00098 return width; 00099 }
1.4.3