Several time control functions (reproduced)

Source: Internet
Author: User

With the rapid development of hardware and software, computer technology has been widely used in the field of automation control, in order to achieve real-time control, the control program must be able to accurately complete timing and timing functions. VC provides a lot of functions about time operations, the following according to their accuracy, respectively, the description.
General Time Control function
VC programmers take advantage of Windows's Wm-timer message map for simple time Control: 1. Call function SetTimer () to set a timing interval, such as SetTimer (0,200,null), to set the time interval of 200 milliseconds ; 2. Add the timer response function OnTimer () to the application and add a response processing statement to the function to complete the time-to-date operation. This timing method is very simple, but its timing function as the sleep () function, like the delay function, the accuracy is low, can only be used to achieve such as the dynamic display of the bitmap timing accuracy requirements are not high, and in the high precision requirements, this method should be avoided.
Precision Time Control function
In cases where the error is not greater than 1 milliseconds, the GetTickCount () function can be used, and the return value of the function is a DWORD type, which represents the time interval elapsed after the computer starts in milliseconds. With the following programming statement, you can achieve a precise timing of 50 milliseconds with an error of less than 1 milliseconds.
DWORD Dwstart, Dwstop;
Start and end values
Dwstop = GetTickCount ();
while (TRUE)
{
Dwstart = Dwstop;
The last stop value becomes the new starting value
Add the appropriate control statement here
Do
{
Dwstop = GetTickCount ();
} while (DwStop-50 < Dwstart);
}
High-precision time-control function
For the general real-time control, the use of the GetTickCount () function can meet the accuracy requirements, but to further improve the timing accuracy, it is necessary to use the QueryPerformanceFrequency () function and QueryPerformanceCounter () Function. These two functions are high-precision time functions provided by the VC for Windows 9X only, and require the computer to support high-precision timers from hardware. The prototypes of the QueryPerformanceFrequency () function and the QueryPerformanceCounter () function are:
BOOL QueryPerformanceFrequency (Large-integer *lpfrequency);
BOOL QueryPerformanceCounter (Large-integer *lpcount);
The data type Large-integer can be either a 8-byte integer or a union structure that is a two 4-byte integer, depending on whether the compiler supports 64-bit. This type is defined as follows:
typedef Union-large-integer
{
struct
{
DWORD LowPart; 4-byte integer number
LONG Highpart; 4-byte integer number
};
Longlong QuadPart;
8-byte integer number
} Large-integer;
Before timing, you should call the QueryPerformanceFrequency () function to get the clock frequency of the machine's internal timers. I use this function on three kinds of pentiumⅱ machines with a frequency of 266, 300 and 333, and the clock frequency is 1193180Hz. Then, the author calls the QueryPerformanceCounter () function before and after the event that requires strict timing, and calculates the exact time of the event experience by using the difference between the counts and the clock frequency obtained two times. 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);
Get the clock frequency of the counter
Dffreq = (double) litmp. QuadPart;
QueryPerformanceCounter (&LITMP);
Get the initial value
QPart1 = litmp. QuadPart;
Sleep (100);
QueryPerformanceCounter (&LITMP);
Get termination value
QPart2 = litmp. QuadPart;
Dfminus = (double) (QPART2-QPART1);
Dftim = Dfminus/dffreq;
Get the corresponding time value
Execute the above program and get the result of dftim=0.097143767076216 (seconds). Careful readers will find that the result of each execution is different, there is a certain difference, this is due to sleep () of its own error.

Several time control functions (reproduced)

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.