C_C ++ timing functions

Source: Internet
Author: User

Summarize the commonly used timing functions on Windows, including five methods with precision of second, millisecond, and microsecond. It can be divided into two types of time () and clock () under the Standard C/C ++, the time () and clock () used by the Standard C/C ++ () it can be used in both Windows and Linux systems.

In Windows, the following APIs are provided: timeGetTime (), GetTickCount (), and QueryPerformanceCounter.

 


[Cpp]
# Include <windows. h>
# Include <time. h> // time_t time () clock_t clock ()
# Include <Mmsystem. h> // timeGetTime ()
# Pragma comment (lib, "Winmm. lib") // timeGetTime ()
 
Int main (int argc, char * argv [])
{
// Use time () to time the second
Time_t timeBegin, timeEnd;
TimeBegin = time (NULL );
Sleep (800 );
TimeEnd = time (NULL );
Printf ("time % d \ n", timeEnd-timeBegin );


// Use clock () for Millisecond Time
Clock_t clockBegin, clockEnd;
ClockBegin = clock ();
Sleep (800 );
ClockEnd = clock ();
Printf ("clock % d \ n", clockEnd-clockBegin );


// Use timeGetTime () for millisecond timing
DWORD dwBegin, dwEnd;
DwBegin = timeGetTime ();
Sleep (800 );
DwEnd = timeGetTime ();
Printf ("timeGetTime % d \ n", dwEnd-dwBegin );


// Use GetTickCount () for Millisecond Time
DWORD dwGTCBegin, dwGTCEnd;
DwGTCBegin = GetTickCount ();
Sleep (800 );
DwGTCEnd = GetTickCount ();
Printf ("GetTickCount % d \ n", dwGTCEnd-dwGTCBegin );


// Use QueryPerformanceCounter () for microseconds
LARGE_INTEGER large_interger;
Double dff;
_ Int64 c1, c2;
// Obtain whether a precise timer is supported. Timer frequency
If (QueryPerformanceFrequency (& large_interger ))
{
Dff = large_interger.QuadPart;
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C1 = large_interger.QuadPart;
Sleep (800 );
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C2 = large_interger.QuadPart;
Printf ("high-precision timer frequency % lf \ n", dff );
Printf ("First timer Value % I64d second timer Value % I64d timer difference % I64d \ n", c1, c2, c2-c1 );
Printf ("Timing % lf millisecond \ n", (c2-c1) * 1000/dff );
Printf ("\ n ");
}
 
Return 0;
}

# Include <windows. h>
# Include <time. h> // time_t time () clock_t clock ()
# Include <Mmsystem. h> // timeGetTime ()
# Pragma comment (lib, "Winmm. lib") // timeGetTime ()

Int main (int argc, char * argv [])
{
// Use time () to time the second
Time_t timeBegin, timeEnd;
TimeBegin = time (NULL );
Sleep (800 );
TimeEnd = time (NULL );
Printf ("time % d \ n", timeEnd-timeBegin );


// Use clock () for Millisecond Time
Clock_t clockBegin, clockEnd;
ClockBegin = clock ();
Sleep (800 );
ClockEnd = clock ();
Printf ("clock % d \ n", clockEnd-clockBegin );


// Use timeGetTime () for millisecond timing
DWORD dwBegin, dwEnd;
DwBegin = timeGetTime ();
Sleep (800 );
DwEnd = timeGetTime ();
Printf ("timeGetTime % d \ n", dwEnd-dwBegin );


// Use GetTickCount () for Millisecond Time
DWORD dwGTCBegin, dwGTCEnd;
DwGTCBegin = GetTickCount ();
Sleep (800 );
DwGTCEnd = GetTickCount ();
Printf ("GetTickCount % d \ n", dwGTCEnd-dwGTCBegin );


// Use QueryPerformanceCounter () for microseconds
LARGE_INTEGER large_interger;
Double dff;
_ Int64 c1, c2;
// Obtain whether a precise timer is supported. Timer frequency
If (QueryPerformanceFrequency (& large_interger ))
{
Dff = large_interger.QuadPart;
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C1 = large_interger.QuadPart;
Sleep (800 );
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C2 = large_interger.QuadPart;
Printf ("high-precision timer frequency % lf \ n", dff );
Printf ("First timer Value % I64d second timer Value % I64d timer difference % I64d \ n", c1, c2, c2-c1 );
Printf ("Timing % lf millisecond \ n", (c2-c1) * 1000/dff );
Printf ("\ n ");
}

Return 0;
}

 

 

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.