这些时间计数器细化语句的目的是什么
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
得到值 2。
2 除以整数值 5,我们将得到零。 但是,除数被转换为双精度值,因此整数值 2 除以双精度值 5.0。 这给了我们一个双重结果,所以 r1 设置为 0.4。
最后将 r0 和 r1 相加,得到最终结果 3.4。
相关文章:
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 主线的等价语句是什么
- 是什么使这个以'+'开头的字符串添加成为有效的语句?
- 在 if 语句中处理多个 or 的更优雅的方法是什么
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 学习时空复杂性时语句XYZ的含义是什么?
- C++:无论如何,有什么可以知道是什么触发了 If 语句
- 语句中分配给 num 的值是什么
- for 循环后的分号是什么意思?为什么第一个循环语句中有 j++
- 这个语句到底是什么意思 stack<int, list<int> > 它与 stack 有何不同<int>?
- 打印此矩阵的 if 语句是什么
- 在使用语句中指定类名的内容是什么
- 当我需要添加一个无用的返回语句时,C++约定是什么
- 当C/C++程序切换语句转到未定义的大小写并且缺少默认值时,它的正确行为是什么:
- 下面的C++语句是什么意思
- 查找语句中的"!=vector.end()"是什么意思?
- 切换或 if 语句 - C++中更清楚的是什么
- 开关语句中的逗号运算符是什么意思
- 条件语句在比较表达式时的基础是什么?
- 在下面的C++语句中放置 const 的好地方是什么