clock_gettime()返回错误的结果(Debian wheezy在VirtualBox) [英] clock_gettime() returns bad results (Debian wheezy on VirtualBox)

查看:139
本文介绍了clock_gettime()返回错误的结果(Debian wheezy在VirtualBox)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用clock_gettime()来监视经过的时间。

 <$ c $ 

c> #include< time.h>
#include< iostream>
#include< math.h>

using namespace std;

int main()
{
//计算的时间vars。
int ns;

//初始结构。
timespec tt;

//获取开始时间。
clock_gettime(CLOCK_MONOTONIC,& tt);
int ns_start = tt.tv_nsec;
int s_start = tt.tv_sec;

//第二次换行的基址。
int ns_base = 1000e6 - ns_start;

while(true)
{
cin.ignore();

//获取时间。
clock_gettime(CLOCK_MONOTONIC,& tt);

//实现/计算回绕。
if(tt.tv_nsec> = ns_start)ns = tt.tv_nsec - ns_start;
else ns = tt.tv_nsec + ns_base;

//显示结果。
cout<< 通过时间:\ts:< tt.tv_sec-s_start<< ms:< round(ns / 1e6)<< endl;
}

return 0;
}



当我按住任何键一段时间, p>

 通过时间:s:1 ms:833 



通过时间: s:2 ms:308
通过时间:s:2 ms:354
通过时间:s:2 ms:415


通过时间:s:2 ms:459
通过时间:s:2 ms:511


通过时间:s:2 ms:566
通过时间:s:2 ms:613


通过时间:s:2 ms:661
通过时间:s:2 ms:712


通过时间:2 ms:762
通过时间:s:2 ms:813


通过时间:s:2 ms:861
通过时间:s:2 ms :920 // crap从这里开始


通过时间:s:3 ms:970
通过时间:s:3 ms:20

b $ b通过时间:s:3 ms:69
通过时间:s:3 ms:124


通过时间:s:3 ms:171
通过时间:s:3 ms:226


通过时间:s:3 ms:272
通过时间:s:3 ms:329
$ b b
通过时间:s:3 ms:372
通过时间:s:3 ms:429


通过时间:s:3 ms:474
通过时间:s:3 ms:528


通过时间:s:3 ms:576
通过时间:s:3 ms:632


通过时间:s:3 ms:679
通过时间:s:3 ms:736


通过时间:s:3 ms:782
通过时间:s:3 ms:835


通过时间:s:3 ms:880
通过时间:s:4 ms:939


通过时间:s:4 ms:982
通过时间:s:4 ms:38


通过时间:s:4 ms :84
通过时间:s:4 ms:143


通过时间:s:4 ms:188
通过时间:s:4 ms:244


通过时间:s:4 ms:291
通过时间:s:4 ms:348


通过时间:s: 4 ms:391
通过的时间:s:4 ms:448


通过时间:s:4 ms:493
通过时间:s:4 ms: 549


通过时间:s:4 ms:594
通过时间:s:4 ms:650

通过时间:s:4 ms :696

通过时间:s:6 ms:259

通过时间:s:7 ms:989

通过查看结果在注释点被弄乱的数字应该是显而易见的。



解决方案

想象一下计时器从1.999秒开始计算。在2.001秒,你的代码会说,1秒和2毫秒已经过去,当真的应该是零秒和2毫秒。这是因为你从当前秒减去起始秒,即使纳秒部分没有超过它的起始值。



你有正确的想法与纳秒环绕。让我们扩展它,以保持秒超过正确的值。这里有一种方法:

  #include< time.h> 
#include< iostream>
#include< math.h>

using namespace std;

int main()
{
//计算的时间vars。
int ns;
int s;

//初始结构。
timespec tt;

//获取开始时间。
clock_gettime(CLOCK_MONOTONIC,& tt);
int ns_start = tt.tv_nsec;
int s_start = tt.tv_sec;

//第二次换行的基址。
int ns_base = 1000e6 - ns_start;

while(true)
{
cin.ignore();

//获取时间。
clock_gettime(CLOCK_MONOTONIC,& tt);

//实现/计算回绕。
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;
}

//显示结果。
cout<< 通过时间:\ts:< s<< ms:< round(ns / 1e6)<< endl;
}

return 0;
}


I am attempting to use clock_gettime() to monitor elapsed time. However it returns bad results.

I tested it with the following:

#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;
}

When I hold any key for a while I get a similar result:

Time Passed:    s: 1 ms: 833



Time Passed:    s: 2 ms: 308
Time Passed:    s: 2 ms: 354
Time Passed:    s: 2 ms: 415


Time Passed:    s: 2 ms: 459
Time Passed:    s: 2 ms: 511


Time Passed:    s: 2 ms: 566
Time Passed:    s: 2 ms: 613


Time Passed:    s: 2 ms: 661
Time Passed:    s: 2 ms: 712


Time Passed:    s: 2 ms: 762
Time Passed:    s: 2 ms: 813


Time Passed:    s: 2 ms: 861
Time Passed:    s: 2 ms: 920 // crap starts here


Time Passed:    s: 3 ms: 970
Time Passed:    s: 3 ms: 20


Time Passed:    s: 3 ms: 69
Time Passed:    s: 3 ms: 124


Time Passed:    s: 3 ms: 171
Time Passed:    s: 3 ms: 226


Time Passed:    s: 3 ms: 272
Time Passed:    s: 3 ms: 329


Time Passed:    s: 3 ms: 372
Time Passed:    s: 3 ms: 429


Time Passed:    s: 3 ms: 474
Time Passed:    s: 3 ms: 528


Time Passed:    s: 3 ms: 576
Time Passed:    s: 3 ms: 632


Time Passed:    s: 3 ms: 679
Time Passed:    s: 3 ms: 736


Time Passed:    s: 3 ms: 782
Time Passed:    s: 3 ms: 835


Time Passed:    s: 3 ms: 880
Time Passed:    s: 4 ms: 939


Time Passed:    s: 4 ms: 982
Time Passed:    s: 4 ms: 38


Time Passed:    s: 4 ms: 84
Time Passed:    s: 4 ms: 143


Time Passed:    s: 4 ms: 188
Time Passed:    s: 4 ms: 244


Time Passed:    s: 4 ms: 291
Time Passed:    s: 4 ms: 348


Time Passed:    s: 4 ms: 391
Time Passed:    s: 4 ms: 448


Time Passed:    s: 4 ms: 493
Time Passed:    s: 4 ms: 549


Time Passed:    s: 4 ms: 594
Time Passed:    s: 4 ms: 650

Time Passed:    s: 4 ms: 696

Time Passed:    s: 6 ms: 259

Time Passed:    s: 7 ms: 989

It should be obvious by looking at the numbers that results are messed up at the point of comment.

Anyone has any ideas as to why this is and how to fix it?

解决方案

Imagine the timer starts at 1.999 seconds. At 2.001 seconds, your code would say that 1 second and 2 ms have elapsed, when really it should be zero seconds and 2 ms. This is because you're subtracting the starting second from the current second, even if the nanosecond part hasn't passed its starting value.

You had the right idea with the nanosecond wraparound. Let's extend that to keep the seconds from getting ahead of the correct value. Here's one way to do it:

#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;
}

这篇关于clock_gettime()返回错误的结果(Debian wheezy在VirtualBox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆