这些时间计数器细化语句的目的是什么

What is the purpose of these time counter refinement statements?

本文关键字:是什么 语句 细化 时间 计数器      更新时间:2023-10-16

我正在尝试实现一个 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得到值 2。

如果整数值

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

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