0

GetTime ヘルパー関数を実装しようとしています。カウントで測定された現在の時間を取得し、次にシステムの 1 秒あたりのカウント数を取得するため、その関係を使用して現在の時間を秒で取得できます。

しかしその後、私が実際に得られない改善コードがいくつかあります。最後の 2 つのステートメントがあるのはなぜですか?

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;
}
4

1 に答える 1

0

これが宿題である場合は、本当に宿題タグでマークする必要があります。

デバッガーでプログラムを実行し、r0 と r1 の値を調べます (または printf を使用します)。これらの値を見れば、2 つの計算が何を行っているかは明らかです。

6/18 編集

計算を簡単にするためにcountsPerSecond、 の値が 5 でtimeInCounts17だとしましょう。17 を 5 で除算すると結果 3 が得られ、これが double にキャストされるため、r0 は値 3.0 に設定されます。timeInCounts / countsPerSecond__int64__int64__int64

計算(timeInCounts/countsPerSecond)*countsPerSecondにより値 15 が得られ、これがtimeInCounts値 2 から差し引かれます。

整数値 2 を整数値 5 で割ると、ゼロになります。 ただし、除数は double にキャストされるため、整数値 2 は double 値 5.0 で除算されます。これにより結果が 2 倍になるため、r1 は 0.4 に設定されます。

最後に r0 と r1 が加算され、最終結果 3.4 が得られます。

于 2011-06-17T18:51:54.203 に答える