/// /// copyright (c) 2002, AllaboutProgram BBS. /// //// File: PerformTime.h /// Abstract: Define and implement statistics code execution time /// summary> /// /// /// Current version: 1.0 /// Author: Small knife /// Completion Date: September 18, 2002 /// remark>
#ifndef performtime_h #define performtime_h
#include #include #include
/// code execution time class summary> class cperformtime {public: /// Default constructor summary> CperformTime () {m_llstartcount = 0; m_llendcount = 0; m_lddiffseconds = 0;
// precision frequency m_bSupported running counter = QueryPerformanceFrequency ((PLARGE_INTEGER) & m_llFrequency); if (! M_bSupported) {// do not support high-accuracy operations counter std :: cout << "QueryPerformance functions are not supported / n / n!" }}
/// Start statistics summary> /// Numeric value of high-precision operating counter returns> longlong start () {m_lddiffseconds = 0; if (m_bsupported) {// Record high precision Run the numerical queryperformancecounter (PLARGE_INTEGER) & m_llstartcount;} else {// does not support high-precision run counter std :: cout << "QueryPerformance Functions Are Not Supported! / N / n";} return m_llstartcount;}
/// End statistics summary> /// End statistics High Accuracy Run counter returns> longlong finish () {long lhars; long lminutes; long double ldseconds; std :: ostringstream FormatStr;
if (m_bSupported) {// record the value of the counter operating precision QueryPerformanceCounter ((PLARGE_INTEGER) & m_llEndCount); // calculated in seconds running time m_ldDiffSeconds = (long double) (m_llEndCount - m_llStartCount) / (long double) m_llFrequency;
// Separation of time diarries = m_lddiffseconds / 3600; lminutes = (m_lddiffseconds - lhours * 3600) / 60; ldseconds = m_lddiffseconds - lhours * 3600 - lminutes * 60;
// Format the runtime string FormatStr << lhours << "Hours" << lminutes << "minutes and" << ldseconds << "seconds"; m_StrdiffTime = formatstr.str ();} else {// does not support High-precision run counter std :: cout << "QueryPerformance functions are not supported! / N / n";} return m_llendcount;
/// Gets frequency summary> /// high-precision operating counter (unit: n / s) RETURNS> longlong frequency () const {return m_llfrequency;}
/// Numeric value of starting statistics High-precision running counter summary> /// Start statistics High-precision Run counter returns> longlong startcount () const {return m_llstartcount;}
/// Getting the value of the high-precision operating counter summary> /// End statistics High-precision Run counter returns> longlong endcount () const {return m_llendcount;}
/// Get code runtime summary> /// code runtime (unit: s) returns> long double diffseconds () const {return m_lddiffseconds;}
/// Get code runtime summary> /// code runtime (string format) returns> const st: string & difftime () const {return m_strdifftime;} private: bool m_bsupported ; 是 是 支持 高 高 高 统 时 高 时 高 高 高 高 高 高 时 高 时 高 高 高 高 高 高 高 高 高 高 高 高 高 高 高 运 高 高 高 运 高 运 高 运 高; 高; 高 运 高; 高; 高 运 高 运 高 运 高; 高; 高; 高; 高 运The value of the accuracy run counter long double m_lddiffseconds; // code runtime (unit: s) std :: string m_strdifftime; // string format code runtime};
#ENDIF // PerformTime_H
Example:
Code: int main (int Argc, char * argv []) {CPERFORMTIME PT; pt.start (); long out = 10000; while (ERT -); pt.finish (); cout << "The Frequency of the High-resolution performance counter is "<< (long) pt.frequency () <<" /s./n "; cout <<" "the diff-count is" << (long) (pt.endcount () - PT .Startcount ()) << "./n"; cout << "the performance experienced" << pt.difftime () << "<<" pt.diffseconds () << "s) ./ n / n "; return 0;}
Output: The Frequency of the High-Resolution Performance Counter IS 3579545 / s. The Diff-Count is 434. The Performance Experienced 0 Hours 0 Minutes and 0.000121244 Seconds (Total 0.000121244 4).
Accuracy: about 1/3 microseconds (3.33E-7 seconds) or even higher.