C++ clock_gettime()返回错误结果(VirtualBox上的Debian喘息)
我正在尝试使用clock_gettime()来监视经过的时间。然而,它返回了糟糕的结果 我用以下方法对其进行了测试:C++ clock_gettime()返回错误结果(VirtualBox上的Debian喘息),c++,linux,timer,C++,Linux,Timer,我正在尝试使用clock_gettime()来监视经过的时间。然而,它返回了糟糕的结果 我用以下方法对其进行了测试: #include <time.h> #include <iostream> #include <math.h> using namespace std; int main() { // Time vars for calculation. int ns; // Initial struct. timespe
#include <time.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
// Time vars for calculation.
int ns;
// Initial struct.
timespec tt;
// Get starting time.
clock_gettime(CLOCK_MONOTONIC,&tt);
int ns_start = tt.tv_nsec;
int s_start = tt.tv_sec;
// Base for second wrap around.
int ns_base = 1000e6 - ns_start;
while(true)
{
cin.ignore();
// Get time.
clock_gettime(CLOCK_MONOTONIC,&tt);
// Implement/calculate wrap around.
if(tt.tv_nsec >= ns_start) ns = tt.tv_nsec - ns_start;
else ns = tt.tv_nsec + ns_base;
// Display result.
cout << "Time Passed:\ts: " << tt.tv_sec-s_start << " ms: " << round(ns/1e6) << endl;
}
return 0;
}
从数字上看,在评论的时候,结果是混乱的,这应该是显而易见的
有人知道这是为什么以及如何解决吗?想象一下计时器在1.999秒启动。在2.001秒时,您的代码会说1秒2毫秒已经过去,而实际上应该是0秒2毫秒。这是因为您正在从当前秒中减去起始秒,即使纳秒部分没有超过其起始值 你对纳秒环绕的想法是对的。让我们扩展它,以防止秒数超过正确的值。这里有一种方法:
#include <time.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
// Time vars for calculation.
int ns;
int s;
// Initial struct.
timespec tt;
// Get starting time.
clock_gettime(CLOCK_MONOTONIC,&tt);
int ns_start = tt.tv_nsec;
int s_start = tt.tv_sec;
// Base for second wrap around.
int ns_base = 1000e6 - ns_start;
while(true)
{
cin.ignore();
// Get time.
clock_gettime(CLOCK_MONOTONIC,&tt);
// Implement/calculate wrap around.
if(tt.tv_nsec >= ns_start)
{
ns = tt.tv_nsec - ns_start;
s = tt.tv_sec - s_start;
}
else
{
ns = tt.tv_nsec + ns_base;
s = tt.tv_sec - s_start - 1;
}
// Display result.
cout << "Time Passed:\ts: " << s << " ms: " << round(ns/1e6) << endl;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
//用于计算的时间变量。
int-ns;
int-s;
//初始结构。
timespec tt;
//开始时间。
时钟获取时间(时钟单调,&tt);
int ns_start=tt.tv_nsec;
int s_start=tt.tv_sec;
//第二次环绕的基础。
int ns_base=1000e6-ns_启动;
while(true)
{
cin.ignore();
//抓紧时间。
时钟获取时间(时钟单调,&tt);
//实施/计算环绕。
如果(tt.tv\U nsec>=ns\U启动)
{
ns=tt.tv\u nsec-ns\u启动;
s=tt.tv_秒-s_开始;
}
其他的
{
ns=tt.tv\U nsec+ns\U基地;
s=tt.tv_秒-s_开始-1;
}
//显示结果。
难道我看不到这种行为,但tv_sec&nsec通常是长的。当您分配给int时,可能是在截断?@Duck这两种类型的实现都使用4字节,因为它是典型的32位程序,所以不会发生这种情况。@RedAlert on Debian wheezy(我相信gcc/g++4.7)它被标记为实验支持:/先生,你太棒了。进来,分析问题,提供最小的(与原始代码的偏差最小),清晰解释的解决方案(带有根本原因推理),让其他人看起来像鸭子。这就是一个真正的专业人士应该做的。谢谢你的回答。
#include <time.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
// Time vars for calculation.
int ns;
int s;
// Initial struct.
timespec tt;
// Get starting time.
clock_gettime(CLOCK_MONOTONIC,&tt);
int ns_start = tt.tv_nsec;
int s_start = tt.tv_sec;
// Base for second wrap around.
int ns_base = 1000e6 - ns_start;
while(true)
{
cin.ignore();
// Get time.
clock_gettime(CLOCK_MONOTONIC,&tt);
// Implement/calculate wrap around.
if(tt.tv_nsec >= ns_start)
{
ns = tt.tv_nsec - ns_start;
s = tt.tv_sec - s_start;
}
else
{
ns = tt.tv_nsec + ns_base;
s = tt.tv_sec - s_start - 1;
}
// Display result.
cout << "Time Passed:\ts: " << s << " ms: " << round(ns/1e6) << endl;
}
return 0;
}