QueryPerformanceFrequency use method--windows High Precision timing count

Source: Internet
Author: User
Tags event timer

on a multi-core or multiprocessor computer. Especially on computers that support CPU frequency dynamic tuning, QueryPerformanceFrequency () under Windows system gets Hpet(assuming existence), while the QueryPerformanceCounter () Get Hpet (assuming high precision event timer (hiPrecision event timer)) The number of clock cycles since power-on, independent of CPU frequency . Previous functionThe different values are not fed back by the difference in the CPU or the frequency of the CPU at different times because the thread executes. The system will not change after power-on initialization。 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.

Suppose you want to get the default information for the CPU. Includes manufacturer, version, default frequency, and so on. Please use: __cpuid (). Assuming that you want to get the actual frequency of the current CPU, get the current base and octave of the system and calculate it.

Note: However, this is related to the detailed execution environment of the program, the smooth and smooth execution of the program takes a short time, the program execution environment is not good, the duration is very long.

Therefore, although high-precision timing counting can be achieved, the execution time of the program is also related to the execution environment, which 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 counter; 0. Hardware not supported. Read failed.

QueryPerformanceFrequency ()-Technical features

High-precision timers for Win9x use: QueryPerformanceFrequency () and QueryPerformanceCounter () . Requires the computer to support high-precision timers from the hardware. Need to include windows.h header files.

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 , the detailed use method depends on whether the compiler supports 64-bit. The definition of the type is 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 call QueryPerformanceCounter () before and after the event that requires a rigorous timing, using the difference in counts and the clock frequency obtained two times. It is possible to calculate the exact time of the event experience.


The exact time to test 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.


QueryPerformanceFrequency use method--windows High Precision timing count

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.