Define and implement the statistical code execution time

xiaoxiao2021-03-06  90

///

/// copyright (c) 2002, AllaboutProgram BBS. /// //// File: PerformTime.h /// Abstract: Define and implement statistics code execution time /// /// /// /// Current version: 1.0 /// Author: Small knife /// Completion Date: September 18, 2002 ///

#ifndef performtime_h #define performtime_h

#include #include #include

///

code execution time class class cperformtime {public: /// Default constructor 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 /// Numeric value of high-precision operating counter 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 /// End statistics High Accuracy Run counter 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 /// high-precision operating counter (unit: n / s) longlong frequency () const {return m_llfrequency;}

///

Numeric value of starting statistics High-precision running counter /// Start statistics High-precision Run counter longlong startcount () const {return m_llstartcount;}

///

Getting the value of the high-precision operating counter /// End statistics High-precision Run counter longlong endcount () const {return m_llendcount;}

///

Get code runtime /// code runtime (unit: s) long double diffseconds () const {return m_lddiffseconds;}

///

Get code runtime /// code runtime (string format) 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.

转载请注明原文地址:https://www.9cbs.com/read-123009.html

New Post(0)