这些时间计数器细化语句的目的是什么?
我正在尝试实现 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;
}
I'm trying to implement a GetTime helper function. It gets the current time, measured in counts, then gets the number of counts per second of the system, so you can get the current time in seconds with its relation.
But after that, there is some improvement code that i dont really get. Why are the last two statements there?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是家庭作业,你真的应该用家庭作业标签来标记它。
在调试器中运行程序并检查值 r0 和 r1(或使用 printf)。一旦您看到这些值,就应该清楚这两个计算在做什么。
编辑 6/18
为了使计算简单,假设
countsPerSecond
的值为 5,timeInCounts
为 17。计算timeInCounts / countsPerSecond
将一个__int64
除以另一个__int64
,因此结果也将是一个__int64
。将 17 除以 5 得到结果 3,然后将其转换为 double,因此 r0 设置为值 3.0。计算
(timeInCounts/countsPerSecond)*countsPerSecond
给出值 15,然后从timeInCounts
中减去该值,得到值 2。如果整数值 2 除以整数值 5 我们将得到零。 但是,除数被转换为双精度型,因此整数值 2 除以双精度值 5.0。这给我们带来了双倍结果,因此 r1 设置为 0.4。
最后将 r0 和 r1 加在一起,得到最终结果 3.4。
If this is homework you should really mark it with the homework tag.
Run the program in a debugger and examine the values r0 and r1 (or use printf). It should be obvious what the two calculations are doing once you see those values.
Edit 6/18
To keep the calculations simple, let's say
countsPerSecond
has the value 5 andtimeInCounts
is 17. The calculationtimeInCounts / countsPerSecond
divides one__int64
by another__int64
so the result will also be an__int64
. Dividing 17 by 5 gives us the result 3 which is then cast to a double so r0 is set to the value 3.0.The calculation
(timeInCounts/countsPerSecond)*countsPerSecond
gives us the value 15 which is then subtracted fromtimeInCounts
giving us the value 2.If the integer value 2 were divided by the integer value 5 we would get zero. But, the divisor is cast to a double so the integer value 2 is divided by the double value 5.0. This gives us a double result, so r1 is set to 0.4.
Finally r0 and r1 are added together giving the final result 3.4.