High-precision timing and High-frequency event generation under Windows

Source: Internet
Author: User
Tags count thread

In the development of applications under Windows, often need to use the timing, especially in some of the more time-demanding programs, the accuracy of the timing is very important, this article introduces two precise timing methods, the accuracy of the timing can be achieved MS level, and can be considered to be accurate, Can be used as a benchmark for time in most cases.

Using API functions:: timeGetTime () to get the number of MS from the boot to the current, its return type is a DWORD type, so its maximum timing length is 2^32ms, about 49 days,:: timeGetTime () is a multimedia function, So its priority is very high, it can generally be considered as accurate.

Using the method of querying system timer's counting value, the API function used is QueryPerformanceCounter, QueryPerformanceFrequency, the method is to use the current count value minus the counting value of starting time, get the count difference value, divided by the frequency of the system timer is the time, usually the frequency of the system timer is very high, I intel845e on the motherboard to reach 3579545hz, of course, for different motherboards, it's frequency is different. The results of the program running are as shown in Figure one:

Figure I

This timing method uses another thread to query the system timer's count value, which uses multithreading knowledge. Because the thread call is to require processor time, so in this, the multithreading timer always lag behind the multimedia timer time. But any time in the middle of a read is very precise, but there is a delay from reading to showing.

Let's talk about the generation of Windows High-frequency events, or the above two methods, Windows has a multimedia timer, used as a set of API function calls, they are:

MMRESULT timeBeginPeriod( UINT uPeriod ) ;
MMRESULT timeSetEvent( UINT uDelay,
            UINT uResolution,
            LPTIMECALLBACK lpTimeProc,
            DWORD dwUser,
            UINT fuEvent
);
void CALLBACK TimeProc( UINT uID,
            UINT uMsg,
            DWORD dwUser,
            DWORD dw1,
            DWORD dw2
);
MMRESULT timeKillEvent( UINT uTimerID );
MMRESULT timeEndPeriod( UINT uPeriod );

Where the timebeginperiod is used to set the highest timing precision, the highest accuracy of 1ms, if you want to produce an interval of 1ms interrupt, must call Timebeginperiod (1), when the timer is used to use Timeendperiod (1); To restore the default precision. The use of the method is to register a callback function with timeSetEvent () after the Timebeginperiod (1) Call, that is, an interrupt processing process. It can also pass a parameter to the callback function, usually passing a window handle or something like that. The callback function Timeproc is used to remove the passed arguments from the Dwwuser parameter. Under Windows, this method can be used for 1ms precision timing data acquisition, data transmission, but to ensure that 1ms can complete all operations and operations. I have proved by practice that it is sufficient to realize the precision of control.

The second approach is to use a multithreaded query system timer count value, it is compared with the above mentioned methods have advantages and disadvantages, the disadvantage is that the precision is not high enough, the advantage is that the interval can break through the 1ms limit, can achieve a smaller interval, the theoretical event can be generated frequency and the frequency of the system timer. The main sample code is as follows:

UINT Timer(LPVOID pParam)
{
  QueryPerformanceCounter((LARGE_INTEGER *)& gl_BeginTime );
  while(gl_bStart)
  {
    QueryPerformanceCounter((LARGE_INTEGER *)&gl_CurrentTime );
    If(gl_CurrentTime - gl_BeginTime > 1.0/Interval )
    {
      //定时的事件,比如发送数据到端口,采集数据等
      gl_BeginTime = gl_CurrentTime;
    }
  }
  return 1;
}

This is a thread function in multiple threads, interval is the interval that generates events, if 0.001 is 1ms, and theoretically if interval is 1, the event is generated at the maximum frequency. That is, you can use Windows to generate very high frequency events, but because the thread call is to have time, sometimes it may cause this thread has not been executed, thus causing a period of time did not count, this time the timing of the event is not produced, if the frequency of the higher the probability of loss is greater. However, it is valuable to use it to produce random signals with varying frequency over time. This is particularly true in real time simulations.

For specific implementations, see the detailed example code.

This article supporting source code

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.