CodeA precise timer is required for optimization. The commonly used gettickcount function can achieve millisecond-level precision. But it is not enough. In this case, you can increase the number of cycles. In addition, there is a high-resolution performance counter (high-resolution Performance Counter), which provides two API functions, obtain the queryperformancefrequency of the counter frequency and queryperformancecounter of the counter value. The implementation principle is implemented by using the 8253,8254 programmable time interval timer chip in the computer. There are three independent 16-bit counters in the computer.
The counter can be counted in binary or decimal (BDC. The counter generates n pulses per second. Each pulse reduces the number of the counter by one and the generation frequency is variable. You can use queryperformancefrequency. For example, my computer (P4 2.8g) returns 2793050000, if P4 3G is used, 2992530000 is returned. Queryperformance counter can get the current counter value. Therefore, as long as your computer is fast enough, the theoretical precision can reach 1/2793050000 seconds or higher.
First look at the high-resolution counter class
Public class counter
{
Protected int64 m_freq;
Protected bool m_useperf = true; // whether the high-resolution counter is available
Public counter ()
{< br> If (queryperformancefrequency (ref m_freq) = 0)
{< br> m_freq = 1000;
m_useperf = false;
}< BR >}
// obtain the current tick
Public int64 currenttick ()
{< br> If (m_useperf)
{< br> int64 curtick = 0;
queryperformancecounter (ref curtick);
return curtick;
}< br>
return (int64) environment. tickcount;
}
// obtain the number of tick answers in two time periods
private int64 getsafedelta (int64 curtime, int64 prevtime, int64 maxvalue)
{< br> If (curtime return curtime + maxvalue-prevtime;
Return curtime-prevtime;
}
// Obtain the time (in milliseconds) between two tick)
Public int64 deltatime_ms (int64 curtick, int64 prevtick)
{
If (m_useperf)
Return 1000 * getsafedelta (curtick, prevtick, int64.maxvalue)/m_freq;
Return getsafedelta (curtick, prevtick, int32.maxvalue );
}
[System. runtime. interopservices. dllimport ("kernel32.dll")]
Internal static extern int queryperformancefrequency (ref int64 lpfrequency );
[System. runtime. interopservices. dllimport ("kernel32.dll")]
Internal static extern int queryperformancecounter (ref int64 lpperformancecount );
}
Application:
Counter = new counter ();
Flag = true;
While (FLAG)
{
Int64 starttick = counter. currenttick ();
Tickcount ++;
If (tickcount % framespersecond = 0)
Label9.text = (tickcount/framespersecond). tostring ();
// Update the screen by Frame
Label3.text = tickcount. tostring ();
Int64 deltams = counter. deltatime_ms (counter. currenttick (), starttick );
Int64 targetms = (int64) (1000.0f * secondsperframe); // If framespersecond cannot be divisible by 1, there will be a travel Error
Dt = DT. addmilliseconds (targetms );
Label1.text = DT. tostring () + "" + dt. millisecond;
If (deltams <= targetms)
{
Label4.text = counter. currenttick (). tostring ();
// Empty loop before the time frame arrives
While (counter. deltatime_ms (counter. currenttick (), starttick) <targetms)
{
Thread. Sleep (0 );
Application. doevents ();
}
}
}
Download complete code