Statistics microsecond running time

When writing code requires statistical algorithm time to find a solution after reading some articles, the reference here is mainly about the integration of the two articles, the original link has been posted on the below.

references:

1 https://www.cnblogs.com/whiteyun/archive/2009/09/17/1568240.html

2,https://blog.csdn.net/nowayings/article/details/44812201

Mainly used LARGE_INTEGER type and QueryPerformanceFrequency () function

LARGE_INTEGER

LARGE_INTEGER a union structure, can be used to represent a 64-bit signed integer value, having the structure:


typedef union _LARGE_INTEGER {
struct {
ULONG LowPart;
LONG HighPart;
} DUMMYSTRUCTNAME;
struct {
ULONG LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;
} LARGE_INTEGER;

LARGE_INTEGER is a consortium. The design is very clever. Consortium of three elements may be considered to define three LARGE_INTEGER

 

(1) DUMMYSTRUCTNAME consists of two parts. It is a lower 32-bit integer LowPart. Another is the high integer slightly. In the case of the small end. The lower 32 bits of digital front. After the 32-bit high.

If the 64-bit integer assigned 100. So to write

Value the LARGE_INTEGER;
value.LowPart = 100;
value.HighPart = 0;
(2) U consists of two parts. It is a lower 32-bit integer LowPart. Another is the high integer slightly. In the case of the big end. The high 32-bit Digital front. After 32 low.
If the 64-bit integer assigned 100. So to write

Value LARGE_INTEGER;
value.u.LowPart = 100;
value.u.HighPart = 0;
(. 3) when the LARGE_INTEGER LONGLONG equivalent. If the 64-bit integer assigned 100. So to write

LARGE_INTEGER value;
value.QuadPart = 100;

If you have a compiler directly supports 64-bit integers can be used QuadPart (64-bit) directly, or respectively LowPart (32-bit) and HighPart (32-bit) access, the highest level HighPart is the sign bit. 
   
Represents the number range: - 3689348814741910324 to +4611686018427387903 
   
value 4000000000 LARGE_INTEGER like layout in memory: 
   
00 28 6B 00 00 00 00 EE        
(low byte) (high byte)   

 

QueryPerformanceFrequency () - the basics

Type: Win32API

原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

Action: return frequency precision hardware support counter.

Return Value: zero, precision hardware support counter; zero, the hardware does not support read failure.
QueryPerformanceFrequency () - technical characteristics used for high-precision timer WIN9X: QueryPerformanceFrequency () and QueryPerformanceCounter (), to support high-precision timer is required from the computer hardware.

Function prototype is:
  BOOL QueryPerformanceFrequency (lpFrequency the LARGE_INTEGER *);
  BOOL the QueryPerformanceCounter (lpCount the LARGE_INTEGER *);

LARGEINTEGER data type as either a 8-byte integers, may be used as two 4-byte integer joint structure, which support 64-bit specific usage The compiler may be. The type is defined as follows:
  typeef the LARGE_INTEGER Union _
  {
   struct
   {
   DWORD LowPart;
   a LONG HighPart;
   };
   The LONGLONG QuadPart;
  } the LARGE_INTEGER;

Before the timing should call QueryPerformanceFrequency () function to obtain the machine clock frequency of the internal timer. Then, after the need for strict timing before the event occurred, respectively, and the QueryPerformanceCounter call (), using the difference between the count and the clock frequency twice obtained, we can calculate the exact time of the event experienced. Test function SLEEP (100) the exact duration of the method:
  the LARGE_INTEGER LiTMP;
  The LONGLONG QT1, Qt2;
  Double DFT, DFF, DFM;
  QueryPerformanceFrequency (& LiTMP); // get frequency clock
  DFF = (Double) litmp.QuadPart;
  the QueryPerformanceCounter (& LiTMP) ; // get the initial value
  QT1 = litmp.QuadPart; Sleep (100);
  the QueryPerformanceCounter (& LiTMP); // get the termination value
  Qt2 = litmp.QuadPart;
  DFM = (Double) (Qt2-QT1);
  DFT = DFM / DFF; // get the time corresponding to the value

Note that the result of the unit DFT calculations in seconds.

Guess you like

Origin www.cnblogs.com/llee-123/p/11203283.html