Summary of various windows timing functions

Source: Internet
Author: User
Tags time in milliseconds
This article summarizes the commonly used timing functions on the Windows platform, 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. Finally, the article provides sample code for five timing methods.

 

Two time functions of the Standard C/C ++: Time () and clock ()

 

Time_t time (time_t * timer );

Returns the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970, based on GMT.

Time_t is actually a long integer typedef long time_t;

Header file: # include <time. h>

 

Clock_t clock (void );

The number of CPU clock units (clock tick) that the process starts to call a function. In msdn, it is called the wall clock time (Wal-clock), in milliseconds.

Clock_t is actually a long integer typedef long clock_t;

Header file: # include <time. h>

 

 

Windows system API functions

Timegettime (), gettickcount (), and queryperformancecounter ()

 

DWORD timegettime (void );

Returns the system time in milliseconds. The system time is the number of milliseconds from system start to function call. Note that this value is a 32-bit value, and will be cyclically between 0 and 2 ^ 32, for about 49.71 days.

Header file: # include <mmsystem. h>

Reference Library: # pragma comment (Lib, "winmm. lib ")

 

DWORD winapi gettickcount (void );

Like timegettime (), this function returns the system time in milliseconds.

Header file: Use # include <windows. h> directly.

 

High-precision timing, in microseconds (1 Ms = 1000 microseconds ).

Let's first look at the definitions of the two functions.

Bool queryperformancecounter (large_integer * lpperformancecount );

Obtain the value of a high-precision timer (if such a timer exists ).

Bool queryperformancefrequency (large_integer * lpfrequency );

Returns the frequency (times per second) of the high-precision counters supported by the hardware. If the return value is 0, the counter fails.

Let's look at large_integer.

It is actually a consortium that can get _ int64 quadpart; it can also get the low 32-bit DWORD lowpart and the high 32-bit value long highpart respectively.

In use, use queryperformancefrequency () to obtain the counter frequency, and then calculate the difference of the timer value obtained by the second call queryperformancecounter (). Divide the difference by the frequency to get the exact timing.

Header file: Use # include <windows. h> directly.

 

 

The following sample code can be tested on your computer.

// In Windows, time (), clock (), timegettime (), gettickcount (), and queryperformancecounter () are used for timing by morewindows # include <stdio. h> # include <windows. h> # include <time. h> // time_t time () clock_t clock () # include <mmsystem. h> // timegettime () # pragma comment (Lib, "winmm. lib ") // timegettime () int main () {// time_t timebegin, timeend; timebegin = Time (null); sleep (1000 ); timeend = Time (null); printf ("% d \ n", timeend-timebegin); // uses clock () to time clock_t clockbegin, clockend; clockbegin = clock (); sleep (800); clockend = clock (); printf ("% d \ n", clockend-clockbegin); // use timegettime () DWORD dwbegin, dwend; dwbegin = timegettime (); sleep (800); dwend = timegettime (); printf ("% d \ n", dwend-dwbegin ); // use gettickcount () to time the DWORD dwgtcbegin, dwgtcend; dwgtcbegin = gettickcount (); sleep (800); dwgtcend = gettickcount (); printf ("% d \ n ", dwgtcend-dwgtcbegin); // use queryperformancecounter () to time the microsecond large_integer large_interger; double DFF ;__ int64 C1, C2; queryperformancefrequency (& large_interger); DFF = timeout; queryperformancecounter (& large_interger); C1 = large_interger.quadpart; sleep (800); 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 ("by morewindows \ n"); Return 0 ;}

The test results on my computer are as follows:

 

 

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/6854764

 

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.