Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the summary of all kinds of timing functions?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces what the summary of various timing functions is like. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

The summary of timing functions commonly used in Windows platform includes five methods with precision of seconds, milliseconds and microseconds. It is divided into two types of time () and clock () under the standard C ~ (+). The time () and clock () used by the standard C ~ + can be used not only in Windows systems, but also in Linux systems. Under the Windows system, it is accomplished by using the API interfaces timeGetTime (), GetTickCount () and QueryPerformanceCounter () provided by Windows. At the end of the paper, the sample codes of five timing methods are given.

Two timing functions time () and clock () of the standard C _

Time_t time (time_t * timer)

Returns the number of seconds that have elapsed between 00:00:00 on January 1, 1970 and the present moment in Greenwich mean time (GMT).

Time_t is actually a long long integer typedef long time_t.

Header file: # include

Clock_t clock (void)

Returns the number of CPU clock timing units (clock tick) that passed when the process started to call the function, called the wall clock time (wal-clock) in MSDN, in milliseconds.

Clock_t is actually a long long integer typedef long clock_t.

Header file: # include

API function of Windows system

TimeGetTime (), GetTickCount () and QueryPerformanceCounter ()

DWORD timeGetTime (VOID)

Returns the system time in milliseconds. The system time is the number of milliseconds that have elapsed since the system started to call the function. Note that this value is 32-bit and will cycle between 0 and 2 ^ 32, about 49.71 days.

Header file: # include

Reference library: # pragma comment (lib, "Winmm.lib")

DWORD WINAPI GetTickCount (void)

This function, like timeGetTime (), returns the system time in milliseconds.

Header file: just use # include directly.

High precision timing in microseconds (1 millisecond = 1000 microseconds).

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

BOOL QueryPerformanceCounter (LARGE_INTEGER * lpPerformanceCount)

Get the value of a high-precision timer (if one exists).

BOOL QueryPerformanceFrequency (LARGE_INTEGER * lpFrequency)

Returns the frequency of hardware-supported high-precision counters (times per second), with 0 indicating failure.

Look at LARGE_INTEGER again.

It is actually a union, and you can get _ _ int64 QuadPart; and low 32-bit DWORD LowPart and high 32-bit LONG HighPart, respectively.

When in use, first use QueryPerformanceFrequency () to get the frequency of the counter, and then calculate the difference between the timer values obtained by the second call to QueryPerformanceCounter (), and subtract the difference with the frequency to get the accurate timing.

Header file: just use # include directly.

Here is the example code, which can be tested on your computer.

View plain

/ / time (), clock (), timeGetTime (), GetTickCount (), QueryPerformanceCounter () in Windows system to time by MoreWindows

# include

# include

# include / / time_t time () clock_t clock ()

# include / / timeGetTime ()

# pragma comment (lib, "Winmm.lib") / / timeGetTime ()

Int main ()

{

/ / use time () to time seconds

Time_t timeBegin, timeEnd

TimeBegin = time (NULL)

Sleep (1000)

TimeEnd = time (NULL)

Printf ("% d\ n", timeEnd-timeBegin)

/ / timing milliseconds with clock ()

Clock_t clockBegin, clockEnd

ClockBegin = clock ()

Sleep (800)

ClockEnd = clock ()

Printf ("% d\ n", clockEnd-clockBegin)

/ / timing milliseconds with timeGetTime ()

DWORD dwBegin, dwEnd

DwBegin = timeGetTime ()

Sleep (800)

DwEnd = timeGetTime ()

Printf ("% d\ n", dwEnd-dwBegin)

/ / timing milliseconds with GetTickCount ()

DWORD dwGTCBegin, dwGTCEnd

DwGTCBegin = GetTickCount ()

Sleep (800)

DwGTCEnd = GetTickCount ()

Printf ("% d\ n", dwGTCEnd-dwGTCBegin)

/ / use QueryPerformanceCounter () to time microseconds

LARGE_INTEGER large_interger

Double dff

_ _ int64 c1, c2

QueryPerformanceFrequency & large_interger)

Dff = large_interger.QuadPart

QueryPerformanceCounter & large_interger)

C1 = large_interger.QuadPart

Sleep (800)

QueryPerformanceCounter & large_interger)

C2 = large_interger.QuadPart

Printf ("Native 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 milliseconds\ n", (c2-C1) * 1000 / dff)

Printf ("By MoreWindows\ n")

Return 0

}

This is how the summary of various timing functions is shared here. I hope the above content can be helpful to you and can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report