MS Windows Timer limits and how to use higher resolution timing means.

xiaoxiao2021-03-06  91

MS Windows Timer limits and how to use higher resolution timing means.

The resolution of Windows Timer (Timer) is limited, from Programming Windows (5th),

In the Win9x series, this limitation is: The counter can only count only 55 milliseconds at a time.

This limit is calculated, the 8086/8088 main frequency 4.772720 MHz divided by 18 times of 2,

18.2, that is, 18.2 times per second. The interval is approximately 55 milliseconds.

At the Winnt Series (including 2000 / XP / Server 2003), only 10 millisecond intervals can be counted once.

For the lowest limit, calculate at a minimum.

In fact, Win32 API provides us with two functions:

Bool queryperformancefrequency

Large_integer * lpfrequency

);

Bool queryperformancecounter

Large_integer * LPPerFormanceCount

);

They can be used to achieve higher resolution timing means.

The first function gets the count value per second of your current CPU, such as 3579545 on my PIII 500MHz.

The second function gets you boot to the current count (Note: The time to sleep is also counting inside.). Two calls can be obtained by requesting the interval of the call.

More specific instructions can be found in MSDN

Below is an application instance, he will sort a sorting algorithm within 1 millisecond, running for 10 seconds, is a 10,000 order algorithm, if you see the console light flashed 10 (1 second), then explain this algorithm The current CPU can be completed within 1 millisecond. However, if the algorithm is more than 1 millisecond, then the running time is definitely more than 10 seconds.

#include

#include

#include

#include

#include

Using namespace std;

Const size_t maxsize = 1000;

Void do_something ()

{

// do something

Vector array (maxsize);

For (size_t i = 0; i

{

Array [i] = rand ()% 9999;

}

Sort (array.begin (), array.end ());

}

int main ()

{

Large_integer begin;

Large_integer end;

Large_integer countspersec; // Current count count

SIZE_T DIVISIONNUM = 1000; // Segmentation of 1 second

LONGLONG IntervalSec; // The number of counts after segmentation, that is, the number of intervals of an algorithm

Static size_t count = 0;

SIZE_T TOTALSEC = 10; // Total number of seconds

QueryperFormanceFrequency (& CountSpersec);

Intervalsec = countspesec.quadpart / divisionNum;

While (1)

{

QueryperFormanceCounter (& BEGIN);

Do_something (); while (1)

{

QueryperFormanceCounter (& End);

IF (End.quadpart - Begin.quadpart> = Intervalsec)

{

Break;

}

}

count;

IF (count == Totalsec * DivisionNum)

{

COUT << "finish" << endl;

Break;

}

}

System ("pause");

}

Exterior: Windows is a universal operating system after all, and is not enough for real-time response to improve high-resolution timers. However, Windows can also be configured

Meet a certain real-time operating system. WindowsXP is working in a real-time extension.

转载请注明原文地址:https://www.9cbs.com/read-122781.html

New Post(0)