General time control functions
VC programmers will use Windows wm_timer message ing for simple time control: 1. call the settimer () function to set the time interval. For example, settimer (0,200, null) sets the time interval of 200 milliseconds. 2. add the scheduled response function ontimer () to the application, and add the Response Processing statement to the function to complete the operation at that time. This timing method is very simple, but its timing function is the same as the delay function of the sleep () function, with low precision, this method can only be used to achieve situations where the timing precision is not high, such as dynamic display of Bitmap. However, this method should be avoided when the precision is high.
Precision Time Control Function
If the error is not greater than 1 millisecond, you can use the gettickcount () function. The return value of this function is DWORD, indicating the time interval after the computer starts in milliseconds. The following programming statement can be used to implement accurate timing within 50 milliseconds, with an error less than 1 millisecond.
DWORD dwstart, dwstop;
// Start value and end value
Dwstop = gettickcount ();
While (true)
{
Dwstart = dwstop;
// The Last termination value is changed to a new start value.
// Add the corresponding control statement here
Do
{
Dwstop = gettickcount ();
} While (dwstop-50 <dwstart );
}
High-precision time control functions
For general real-time control, the use of the gettickcount () function can meet the precision requirements, but to further improve the timing accuracy, we need to use the queryperformancefrequency () function and queryperformancecounter () function. These two functions are high-precision time functions provided by VC for Windows 9x only, and require computers to support high-precision timers from hardware. The prototype of the queryperformancefrequency () function and queryperformancecounter () function is:
Bool queryperformancefrequency (large_integer * lpfrequency );
Bool queryperformancecounter (large_integer * lpcount );
The Data Type Large-integer can be an integer that is 8 bytes long or a combination of two four-byte long integers, the specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:
Typedef union _ large_integer
{
Struct
{
DWORD lowpart; // 4-byte integer
Long highpart; // 4-byte integer
};
Longlong quadpart;
// 8-byte integer
} Large_integer;
In some computer hardware systems, high-precision operation counters (high-resolution Performance Counter) can be used to obtain high-precision timing intervals, and their accuracy is related to the clock frequency of the CPU. The steps for using this method are as follows: |
1. First, call the queryperformancefrequency function to obtain the frequency f of the high-precision running counter. The Unit is the number of times per second (N/S), which is generally large. |
2. Call queryperformancecounter at both ends of the Code that requires timing to obtain the values N1 and N2 of the high-precision running counter. The difference between the two values is converted to the time interval by F, t = (n2-n1)/F. |
Before timing, call the queryperformancefrequency () function to obtain the clock frequency of the machine's Internal timer. I use this function on three Pentium ⅱ machines with clock speed of 266, 300, and 333. The clock frequency obtained is 1193180Hz. Next, I call the queryperformancecounter () function before and after events that require strict timing, and use the difference in counting and clock frequency obtained twice, you can calculate the exact time of the event experience. The following procedure is used to test the exact duration of the function sleep (100.
Large_integer litmp;
Longlong qpart1, qpart2;
Double dfminus, dffreq, dftim;
Queryperformancefrequency (& litmp );
// Obtain the counter clock frequency
Dffreq = (double) litmp. quadpart;
Queryperformancecounter (& litmp );
// Obtain the Initial Value
Qpart1 = litmp. quadpart;
Sleep (100 );
Queryperformancecounter (& litmp );
// Obtain the end value
Qpart2 = litmp. quadpart;
Dfminus = (double) (qpart2-qpart1 );
Dftim = dfminus/dffreq;
// Obtain the corresponding time value
Run the above program and the result is dftim = 0.097143767076216 (seconds ). Careful readers will find that the results of each execution are different and there are some differences, which is caused by the sleep () error.