繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM