// Windows API in WINBASE. h
Code
//
// Performance counter API's
//
Winbaseapi
Bool
Winapi
Queryperformancecounter (
_ Out large_integer * lpperformancecount
);
Winbaseapi
Bool
Winapi
Queryperformancefrequency (
_ Out large_integer * lpfrequency
);
// Perftimer. h
Code
# Ifdef _ debug
# Define tracefntime (szfunctionname) debugtimer atimer (szfunctionname)
# Else
# Define tracefntime (szfunctionname)
# Endif
//////////////////////////////////////// //////////////////////////////////
// Using the difference of the CPU tick count to divide the CPU frequency per second
// Time_span = (difference of CPU tick count)/(CPU frequency): (seconds)
//////////////////////////////////////// //////////////////////////////////
Class afx_ext_class cperftimer
{
PRIVATE:
Large_integer m_lifreq;
Large_integer m_listart;
Large_integer m_liend;
Public:
Cperftimer (void) //: // m_lifreq.quadpart (0) //, m_listart.quadpart (0), m_liend.quadpart (0)
{
Queryperformancefrequency (& m_lifreq); // CPU frequency
}
~ Cperftimer (void)
{};
Public:
Inline void start ()
{
Queryperformancecounter (& m_listart); // CPU tick that starts timing
}
Inline void stop ()
{
Queryperformancecounter (& m_liend); // CPU tick
};
Inline double gettimespan () // time unit is second
{
Return (double) (m_liend.quadpart-m_listart.quadpart)/(double) m_lifreq.quadpart;
}
};
// Use the Construction and Analysis of temporary objects to calculate the function running time
Class afx_ext_class debugtimer
{
Public:
Explicit debugtimer (lpctstr lpszfuncname)
: M_szfunction (lpszfuncname)
{
M_theperftimer.start ();
}
~ Debugtimer ()
{
M_theperftimer.stop ();
// Print to output
Trace2 ("% s: %. 12f \ r \ n", m_szfunction, m_theperftimer.gettimespan ());
}
PRIVATE:
Cperftimer m_theperftimer;
Lpctstr m_szfunction;
};