Precise Timing using Windows APIs

Source: Internet
Author: User

// 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;
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.