|
A counter is a general term used in programming to refer to an incrementing variable. Some systems include a high-resolution performance counter that provides high-resolution elapsed times.
If a high-resolution performance counter exists on the system, the QueryPerformanceFrequency function can be used to express the frequency, in counts per second. The value of the count is processor dependent. On some processors, for example, the count might be the cycle rate of the processor clock.
The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter (if one exists on the system). By calling this function at the beginning and end of a section of code, an application essentially uses the counter as a high-resolution timer. For example, suppose that QueryPerformanceFrequency indicates that the frequency of the high-resolution performance counter is 50,000 counts per second. If the application calls QueryPerformanceCounter immediately before and immediately after the section of code to be timed, the counter values might be 1500 counts and 3500 counts, respectively. These values would indicate that .04 seconds (2000 counts) elapsed while the code executed.
윗글은 MSDN에 있는내용입니다.
아래는 코드구루에서 가져온 class입니다.
class CElapsed
{
private :
int Initialized;
__int64 Frequency;
__int64 BeginTime;
public :
CElapsed()
{
Initialized = QueryPerformanceFrequency((LARGE_INTEGER*)&Frequency);
}
BOOL Begin()
{
if(!Initialized )
return 0;
return QueryPerformanceCounter((LARGE_INTEGER*)&BeginTime);
}
double End()
{
if(!Initialized )
return 0.0;
__int64 endtime;
QueryPerformanceCounter((LARGE_INTEGER*)&endtime);
__int64 elapsed = endtime - BeginTime;
return (double)elapsed / (double)Frequency;
}
BOOL Available()
{
return Initialized;
}
__int64 GetFreq()
{
return Frequency;
}
};
end 부분을 수정(while loop을 이용)하면 조금더 정확한 delay를 얻을 수 있습니다.
window os의 한계로 정확한 시간을 멈춘다는 것은 어렵지 않겠나 싶군요.
별다섯개 님이 쓰신 글 :
: mS단위의 정확한 delay를 구현하는 방법이 있나요? 혹은 uS까지도 가능한지 모르겠지만요.
:
: void delay(int d)
: {
: double a,b,c;
:
: a = timeGetTime();
:
: while(1)
: {
: b = timeGetTime();
: c = b-a;
: if(c>=delay)break;
: //Application->ProcessMesages();
: }
: }
:
: 이런식의 함수는 window의 다른 process들이 같이 돌기 때문에 정확한 시간을 구현하지
: 못하겠더라구요..
|