[英]What is the purpose of these time counter refinement statements?
我正在嘗試實現GetTime助手功能。 它獲取當前時間(以計數為單位),然后獲取系統每秒的計數數,因此您可以獲取當前時間(以秒為單位)及其關系。
但是在那之后,我確實沒有得到一些改進代碼。 為什么最后兩個語句在那里?
double GetTime()
{
// Current time value, measured in counts
__int64 timeInCounts;
QueryPerformanceCounter((LARGE_INTEGER *)(&timeInCounts));
// To get the frequency (counts per second) of the performance timer
__int64 countsPerSecond;
QueryPerformanceFrequency((LARGE_INTEGER *)(&countsPerSecond));
double r, r0, r1;
// Get the time in seconds with the following relation
r0 = double ( timeInCounts / countsPerSecond );
// There is some kind of acuracy improvement here
r1 = ( timeInCounts - ((timeInCounts/countsPerSecond)*countsPerSecond))
/ (double)(countsPerSecond);
r = r0 + r1;
return r;
}
如果這是家庭作業,則應真正在作業標簽上進行標記。
在調試器中運行程序,並檢查值r0和r1(或使用printf)。 看到這些值后,這兩個計算在做什么很明顯。
編輯6/18
為了簡化計算,假設countsPerSecond
的值為5, timeInCounts
為17。計算timeInCounts / countsPerSecond
將一個__int64
除以另一個__int64
因此結果也將是__int64
。 用17除以5得到的結果為3,然后將其轉換為雙精度,因此將r0設置為值3.0。
計算(timeInCounts/countsPerSecond)*countsPerSecond
給我們值15,然后從timeInCounts
減去它timeInCounts
值2。
如果將整數2除以整數5,我們將得到零。 但是 ,除數被強制轉換為雙精度,因此整數值2除以雙精度值5.0。 這使我們得到了兩倍的結果,因此r1設置為0.4。
最后,將r0和r1相加,得出最終結果3.4。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.