-------------------------------------------------------------------------------
Timing or timing methods in Windows
--------------------------------------------------------------------------------
---- With the rapid development of software and hardware, computer technology has been widely used in the field of automation control. To achieve real-time control, the control program should be able to accurately complete timing and timing. Visual c ++ provides many time-related functions. The following describes the precision of these functions.
---- Any visual C ++ programmer will use Windows wm_timer message ing for simple time control: 1. Call the settimer () function to set the scheduled interval, such as settimer (0,200, null) that is, set the interval of 200 milliseconds. 2. Add the ontimer () scheduled response function to the application, and add the Response Processing statement to the function, it is used to complete the operation at the scheduled time. This timing method is very simple, but its timing function is the same as the delay function of the sleep () function, the accuracy is very low, 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.
---- If the error is not greater than 1 millisecond, you can use the gettickcount () function (if the reader still uses windows3.1, you can use the getcurrenttime () 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. The following statements are used in the control program of the hydraulic servo multi-direction Irregular Wave Generator System developed by the State Key Laboratory of coastal and offshore engineering of Dalian University of Technology for the Guangdong Provincial Institute of Water Conservancy and Hydropower Science.
DWORD dwstart, dwstop; // start value and stop Value
Dwstop = gettickcount ();
While (true)
{
Dwstart = dwstop; // The last stop value is changed to a new start value.
//...... Add the corresponding control statement here ......
Do
{
Dwstop = gettickcount ();
} While (dwstop-50 <dwstart );
}
---- For general real-time control, the gettickcount () function can meet the precision requirements. However, when writing a fast counting program for Dalian kekang, the author found that using the gettickcount () function has a great impact on the counting result. To further improve the timing accuracy, the authors use the queryperformancefrequency () function and queryperformancecounter () function. These two functions are high-precision time functions provided by Visual C ++ only for Windows 95 and later versions, and require computers to support high-precision timers on 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;
---- Before timing, call the queryperformancefrequency () function to obtain the clock frequency of the timer in the machine. We use this function on three Pentium ⅱ machines with clock speed of 266, 300, and 333. The clock frequency obtained is 1193180Hz. Then, the author calls the queryperformancecounter () function before and after events that require strict timing, and uses the difference in counting and clock frequency obtained twice, you can calculate the exact time of the event experience. The following program 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 );
// Get the abort Value
Qpart2 = litmp. quadpart;
Dfminus = (double) (qpart2-qpart1 );
Dftim = dfminus/dffreq;
// Obtain the corresponding time value
---- Execute 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. This is due to sleep () error.
---- This article introduces three implementation methods of timing or timing. You can choose based on your actual situation to implement the Program's timing and timing functions. The above programs are all debugged in Windows 98 using Visual C ++ 5.0 and 6.0.
Ctime T = ctime: getcurrenttime ();
You can get the current time, but it cannot be accurate to milliseconds. To get the time in milliseconds, it generally means that the program runs to the current time or the delay time can be used by the following functions:
DWORD dwstart = gettickcount ();
Bool queryperformancefrequency (
Large_integer * lpfrequency // current frequency
);
Bool queryperformancecounter (
Large_integer * lpperformancecount // counter value
);