0

我正在尝试实现 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;
}
4

1 回答 1

0

如果这是家庭作业,你真的应该用家庭作业标签来标记它。

在调试器中运行程序并检查值 r0 和 r1(或使用 printf)。一旦看到这些值,这两个计算的作用就应该很明显了。

编辑 6/18

为使计算简单,假设countsPerSecond值为 5 和timeInCounts17。计算timeInCounts / countsPerSecond将一个__int64除以另一个__int64,因此结果也将是__int64. 将 17 除以 5 得到结果 3,然后将其转换为双精度数,因此 r0 设置为值 3.0。

计算为我们提供了值 15,然后从给我们的值 2 中 (timeInCounts/countsPerSecond)*countsPerSecond减去该值。timeInCounts

如果整数值 2 除以整数值 5,我们将得到零。 但是,除数被转换为双精度值,因此整数值 2 除以双精度值 5.0。这给了我们一个双倍的结果,所以 r1 设置为 0.4。

最后将 r0 和 r1 相加,得到最终结果 3.4。

于 2011-06-17T18:51:54.203 回答