gettimeofday()C ++不一致 [英] gettimeofday() C++ Inconsistency

查看:114
本文介绍了gettimeofday()C ++不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个涉及比较编程语言的项目.我正在计算Ackermann函数.我测试了Java,Python和Ruby,并获得了10到30毫秒之间的响应.但是C ++似乎需要125毫秒.这是正常现象还是gettimeofday()的问题? Gettimeofday()准时.h.

I'm doing a project that involves comparing programming languages. I'm computing the Ackermann function. I tested Java, Python, and Ruby, and got responses between 10 and 30 milliseconds. But C++ seems to take 125 milliseconds. Is this normal, or is it a problem with the gettimeofday()? Gettimeofday() is in time.h.

我正在(虚拟)Ubuntu Natty Narwhal 32位上进行测试.我的处理能力并不差(四核2.13 GHz Intel Xeon).

I'm testing on a (virtual) Ubuntu Natty Narwhal 32-bit. I'm not short processing power (Quad-core 2.13 GHz Intel Xeon).

我的代码在这里:

#include <iostream>
#include <sys/time.h>
using namespace std;
int a(int m,int n) {
    if (m == 0) {
    return n + 1;
    } else if (m > 0 and n == 0) {
    return a(m-1,1);
    } else if (m > 0 and n > 0) {
    return a(m-1,a(m,n-1));
    }
}

int main() {
    timeval tim;
    gettimeofday(&tim,NULL);
    double t1 = tim.tv_usec;
    int v = a(3,4);           
    gettimeofday(&tim,NULL);
    double t2 = tim.tv_usec;
    cout << v << endl << t2-t1;
    return 0;
}       

推荐答案

假设您正在谈论返回的数据分辨率. org/onlinepubs/009604599/functions/gettimeofday.html"rel =" nofollow noreferrer>用于gettimeofday状态的POSIX规范:

Assuming you're talking about the resolution of the data returned, the POSIX specification for gettimeofday states:

未指定系统时钟的分辨率.

The resolution of the system clock is unspecified.

这是由于以下事实:系统跟踪小时间段的能力可能相差很大.甚至ISO标准的clock()函数都包括这样的警告.

This is due to the fact that systems may have a widely varying capacity for tracking small time periods. Even the ISO standard clock() function includes caveats like this.

如果您谈论的是 long 的调用方式,则该标准不能保证这些方面的性能.一个实现完全免费,可以等待125分钟,然后再给您时间,尽管我怀疑这样的实现是否会在市场上大获成功:-)

If you're talking about how long it takes to call it, the standard makes no guarantees about performance along those lines. An implementation is perfectly free to wait 125 minutes before giving you the time although I doubt such an implementation would have much market success :-)

作为有限分辨率的示例,我输入了以下代码以在系统上对其进行检查:

As an example of the limited resolution, I typed in the following code to check it on my system:

#include <stdio.h>
#include <sys/time.h>

#define NUMBER 30

int main (void) {
    struct timeval tv[NUMBER];
    int count[NUMBER], i, diff;

    gettimeofday (&tv[0], NULL);

    for (i = 1; i < NUMBER; i++) {
        gettimeofday (&tv[i], NULL);
        count[i] = 1;
        while ((tv[i].tv_sec == tv[i-1].tv_sec) &&
            (tv[i].tv_usec == tv[i-1].tv_usec))
        {
            count[i]++;
            gettimeofday (&tv[i], NULL);
        }
    }

    printf ("%2d: secs = %d, usecs = %6d\n", 0, tv[0].tv_sec, tv[0].tv_usec);
    for (i = 1; i < NUMBER; i++) {
        diff = (tv[i].tv_sec - tv[i-1].tv_sec) * 1000000;
        diff += tv[i].tv_usec - tv[i-1].tv_usec;

        printf ("%2d: secs = %d, usecs = %6d, count = %5d, diff = %d\n",
            i, tv[i].tv_sec, tv[i].tv_usec, count[i], diff);
    }

    return 0;
}

该代码基本上记录了底层时间的变化,并记录了实际变化时间对gettimeofday()进行了多少次调用.这是在功能相当强大的机器上,因此它的处理能力也不差(该计数表明它每次能够调用gettimeofday()的频率大约为5,800,而忽略了第一个,因为我们不知道在那个量子中的什么时候开始测量.

The code basically records the changes in the underlying time, keeping a count of how many calls it took to gettimeofday() for the time to actually change. This is on a reasonably powerful machine so it's not short on processing power (the count indicates how often it was able to call gettimeofday() for each time quantum, around the 5,800 mark, ignoring the first since we don't know when in that quantum we started the measurements).

输出为:

 0: secs = 1318554836, usecs = 990820
 1: secs = 1318554836, usecs = 991820, count =  5129, diff = 1000
 2: secs = 1318554836, usecs = 992820, count =  5807, diff = 1000
 3: secs = 1318554836, usecs = 993820, count =  5901, diff = 1000
 4: secs = 1318554836, usecs = 994820, count =  5916, diff = 1000
 5: secs = 1318554836, usecs = 995820, count =  5925, diff = 1000
 6: secs = 1318554836, usecs = 996820, count =  5814, diff = 1000
 7: secs = 1318554836, usecs = 997820, count =  5814, diff = 1000
 8: secs = 1318554836, usecs = 998820, count =  5819, diff = 1000
 9: secs = 1318554836, usecs = 999820, count =  5901, diff = 1000
10: secs = 1318554837, usecs =    820, count =  5815, diff = 1000
11: secs = 1318554837, usecs =   1820, count =  5866, diff = 1000
12: secs = 1318554837, usecs =   2820, count =  5849, diff = 1000
13: secs = 1318554837, usecs =   3820, count =  5857, diff = 1000
14: secs = 1318554837, usecs =   4820, count =  5867, diff = 1000
15: secs = 1318554837, usecs =   5820, count =  5852, diff = 1000
16: secs = 1318554837, usecs =   6820, count =  5865, diff = 1000
17: secs = 1318554837, usecs =   7820, count =  5867, diff = 1000
18: secs = 1318554837, usecs =   8820, count =  5885, diff = 1000
19: secs = 1318554837, usecs =   9820, count =  5864, diff = 1000
20: secs = 1318554837, usecs =  10820, count =  5918, diff = 1000
21: secs = 1318554837, usecs =  11820, count =  5869, diff = 1000
22: secs = 1318554837, usecs =  12820, count =  5866, diff = 1000
23: secs = 1318554837, usecs =  13820, count =  5875, diff = 1000
24: secs = 1318554837, usecs =  14820, count =  5925, diff = 1000
25: secs = 1318554837, usecs =  15820, count =  5870, diff = 1000
26: secs = 1318554837, usecs =  16820, count =  5877, diff = 1000
27: secs = 1318554837, usecs =  17820, count =  5868, diff = 1000
28: secs = 1318554837, usecs =  18820, count =  5874, diff = 1000
29: secs = 1318554837, usecs =  19820, count =  5862, diff = 1000

表明分辨率似乎限制为不超过一千微秒.当然,您的系统可能与此不同,最重要的是,它取决于您的实现和/或环境.

showing that the resolution seems to be limited to no better than one thousand microseconds. Of course, your system may be different to that, the bottom line is that it depends on your implementation and/or environment.

一种避免这种限制的方法是不做任何事情,而是做N次,然后将经过的时间除以N.

One way to get around this type of limitation is to not do something once but to do it N times and then divide the elapsed time by N.

例如,假设您调用函数,而计时器则说花费了125毫秒,您怀疑这似乎有点高.我建议然后将其循环调用一千次,测量整整一千次所花费的时间.

For example, let's say you call your function and the timer says it took 125 milliseconds, something that you suspect seems a little high. I would suggest then calling it a thousand times in a loop, measuring the time it took for the entire thousand.

如果事实证明是125秒,那么可能会很慢.但是,如果仅花费27秒,则表明您的计时器分辨率是造成看似较长时间的原因,因为这相当于每次迭代需要27毫秒,与您从其他结果中看到的结果相当.

If that turns out to be 125 seconds then, yes, it's probably slow. However, if it takes only 27 seconds, that would indicate your timer resolution is what's causing the seemingly large times, since that would equate to 27 milliseconds per iteration, on par with what you're seeing from the other results.

修改代码以考虑到这一点,将遵循以下原则:

Modifying your code to take this into account would be along the lines of:

int main() {
    const int count = 1000;
    timeval tim;

    gettimeofday(&tim, NULL);
    double t1 = 1.0e6 * tim.tv_sec + tim.tv_usec;

    int v;
    for (int i = 0; i < count; ++i)
        v = a(3, 4);           

    gettimeofday(&tim, NULL);
    double t2 = 1.0e6 * tim.tv_sec + tim.tv_usec;

    cout << v << '\n' << ((t2 - t1) / count) << '\n';

    return 0;
}

这篇关于gettimeofday()C ++不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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