2011-07-20

電腦上如何計算高精度的時間差

QueryPerformanceFrequency(…) and QueryPerformanceCounter(…) function
先調用 QueryPerformanceFrequency 來讀取計時器的頻率.
再用兩個 QueryPerformanceCounter 來計算 兩個時間差, 再用 Frequency 正確計算差幾秒鐘.
Ex: Sleep (1000)

LARGE_INTEGER m_swFreq, m_swStart, m_swStop; //stopwatch
double dfMinus, dfFreq, dfTim;

QueryPerformanceFrequency(&m_swFreq);
// 獲得計數器的時鐘頻率
dfFreq = (double)m_swFreq.QuadPart;

QueryPerformanceCounter(&m_swStart);
// 獲得初始值
Sleep(1000) ;
QueryPerformanceCounter(&m_swStop);
// 獲得終止值

dfMinus = (double)(m_swStop.QuadPart- m_swStart.QuadPart);
dfTim = dfMinus / dfFreq;

沒有留言:

AdSense for Search

Google
 

Adsense Content