QueryPerformanceFrequency usage--windows high-precision timing counting

Source: Internet
Author: User
Tags event timer

On a multi-core or multiprocessor computer, especially on a computer that supports dynamic CPU frequency tuning, QueryPerformanceFrequency () under Windows system gets Hpet(if present), while the QueryPerformanceCounter () gets Hpet (if there is a high precision event timer (hiPrecision event timer)) The number of clock cycles since power-on, independent of CPU frequency . Previous functiondoes not respond to different values due to the different CPU or CPU frequency at different times, the system does not change after the power is initialized, the source of the latter function is also uniform. This makes it possible to accurately calculate the performance of the target program, especially the multi-threaded programs.

If you want to get the default information for the CPU, including the manufacturer, version number, default frequency, and so on, use: __cpuid (). If you want to get the actual frequency of the current CPU, get the current base and multiplier of the system and calculate it.

Note: However, this is related to the specific operating environment of the program, the program runs smoothly, the time is short, the program runs in a bad environment and lasts for a long time.

It is possible to achieve a high-precision timing count, but the execution time of the program is still related to the operating environment and cannot be accurately measured.

Precise acquisition Time:

QueryPerformanceFrequency ()-Basic introduction

Type: Win32API

prototype: BOOL QueryPerformanceFrequency (Large_integer *lpfrequency);

function: Returns the high precision of hardware support counter the frequency.

Return value: Non-zero, hardware support high precision counters; 0, hardware not supported, read failed.

QueryPerformanceFrequency ()-Technical features

High-precision timers for Win9x use: QueryPerformanceFrequency () and QueryPerformanceCounter () , requiring the computer to support high-precision timers from hardware. Need to include the Windows.h header file.

The prototype of the function is:

BOOL QueryPerformanceFrequency (Large_integer *lpfrequency);

BOOL QueryPerformanceCounter (Large_integer *lpcount);

The data type Largeinteger can be either a 8-byte long Integer or a two 4-byte long integer. Joint Structure , and its specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:

Typeef Union _ Large_integer

{

struct

{

DWORD LowPart;

LONG Highpart;

};

Longlong QuadPart;

} Large_integer;


The QueryPerformanceFrequency () function should be called before timing to obtain the clock frequency of the internal timer of the machine. Then QueryPerformanceCounter () is called before and after the event that requires strict timing, and the exact time of the event can be calculated using the difference in counts and the clock frequency obtained two times.


To test the exact time of sleep:

#include <stdio.h>

#include <windows.h>

void Main ()

{

Large_integer Nfreq;

Large_integer Nbegintime;

Large_integer Nendtime;

Double time;

QueryPerformanceFrequency (&nfreq);

QueryPerformanceCounter (&nbegintime);

Sleep (1000);

QueryPerformanceCounter (&nendtime);

Time= (Double) (Nendtime.quadpart-nbegintime.quadpart)/(double) Nfreq.quadpart;

printf ("%f\n", time);

Sleep (1000);

System ("Pause");

}

Result is

0.999982

1.000088

1.000200

And so on, so the accuracy of sleep is still relatively low.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

QueryPerformanceFrequency usage--windows high-precision timing counting

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.