Windows performance Timer

Source: Internet
Author: User

Large_integer
Large_integer is union, which indicates a 64-bit signed integer. Other definitions are as follows:

View code

 
Typedef union _ large_integer {
Struct{
DWORD lowpart;
Long highpart;
};
Longlong quadpart;
} Large_integer;

If you have a compiler that supports 64-bit integers, you can use quadpart (64-bit) directly. Otherwise, you can access lowpart (32-bit) and highpart (32-bit) separately. The highest bit of highpart is the symbol bit.
Indicates the number range: -- 3689348814741910324 to + 4611686018427387903
The value of large_integer is equal to 4000000000 in the memory layout:
00 28 6B EE 00 00 00 00
(Low byte) (high byte)

Queryperformancefrequency ()
Type: WIN32API
Prototype: bool queryperformancefrequency (large_integer * lpfrequency );
Purpose: return the hardware-supported high-precision counter frequency.
Returned value: non-zero. Hardware supports high-precision counters. Zero, hardware does not support, and reading fails.
Queryperformancefrequency ()-a high-precision timer with technical features for Win9x: queryperformancefrequency () and queryperformancecounter (), requiring computers to support high-precision timers on hardware.
The original form of the function is:
Bool queryperformancefrequency (large_integer * lpfrequency );
Bool queryperformancecounter (large_integer * lpcount );
The data type largeinteger can be an 8-byte long integer or a union structure of two 4-byte long integers. The specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:

View code

 
Typeef union _ large_integer
{
Struct
{
DWORD lowpart;
Long highpart;
};
Longlong quadpart;
} Large_integer;

Call the queryperformancefrequency () function to obtain the clock frequency of the timer. Then, call queryperformancecounter () before and after the occurrence of an event that requires strict timing. the precise time of the event experience can be calculated using the difference in counting and clock frequency obtained twice. The exact duration method of the test function sleep (100:

View code

Large_integer litmp;
Longlong qt1, qt2;
DoubleDFT, DFF, DFM;
Queryperformancefrequency (& litmp );//Obtain clock frequency
DFF = (Double) Litmp. quadpart;
Queryperformancecounter (& litmp );//Obtain the Initial Value
Qt1 = litmp. quadpart; sleep (100);
Queryperformancecounter (& litmp );//Get termination value
Qt2 = litmp. quadpart;
DFM = (Double) (Qt2-qt1 );
DFT = DFM/DFF;//Obtain the corresponding time value

Note that the unit of result for DFT calculation is second.

[Thank you for your reference]
Http://www.cnblogs.com/whiteyun/archive/2009/09/17/1568240.html
Http://www.cppblog.com/deane/articles/113151.html

Supplement: My understanding of the above content can only be usedTimer, Cannot be usedTimer. (Is there a difference between a timer and a timer ?)
In addition, I have read the materials including getsystemtimeasfiletime () and clock (). Both of them obtain the current time for timing, so it is not very accurate, and the same effect as sleep. See the following

View code

# Include <stdio. h>
# Include <windows. h>
# Include <time. h>

Int Main ( Int Argc, Char * Argv [])
{
Filetime beg, end;
Getsystemtimeasfiletime (& beg );
Sleep ( 2000 );
Getsystemtimeasfiletime (& End );

Clock_t cbeg, Cend;
Cbeg = clock ();
Sleep ( 2000 );
Cend = clock ();

Large_integer start;
Large_integer endt;
Large_integer frequency;
If (! Queryperformancefrequency (& frequency ))
{
Return -1 ;
}
Queryperformancecounter (& START );
Sleep ( 2000 );
Queryperformancecounter (& endt );

Printf ( " % D NS \ n " , 100 * (End. dwLowDateTime-beg.dwLowDateTime ));
Printf ( " % D Ms \ n " , Cend-cbeg );
Printf (" Main cost: % F \ n " ,( Double ) (Endt. quadpart-start. quadpart )/( Double ) Frequency. quadpart );
Return 0 ;
}

The running result is:

Large_integer value during running: (note the value of the member)

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.