【问题标题】:boost timer same results in "for" loop?提升计时器同样会导致“for”循环?
【发布时间】:2013-07-12 17:27:32
【问题描述】:

我正在测试我的计算机的数据对齐速度。测试很简单,处理同一个缓冲区,一次取 2 个字节,一次取 4 个字节,然后是 8 个字节。使用 2 字节访问处理剩余的(如果存在)几个字节。 测试详细描述here

我的问题是当我在循环中执行此操作时,我得到完全相同的 printf 结果经过时间 - 这显然不是真的,当我在循环开始时设置断点时得到确认 - 然后新结果似乎是正确的。所以我认为这必须做一些关于编译器优化的事情(但我为 g++ 设置了 -O0 标志)但我不知道具体是什么,我不明白。

g++ 4.4.7 Ubuntu 12.10 NetBeans AMDx64

那我该怎么办?

for (int i = 0; i < 10; i++) {
    int* itab=new int[1000*256]; //1MiB table  
    double elapsed=0;
    boost::timer* t=new boost::timer();
    Munge16(itab, 250);
    elapsed = t->elapsed();
    printf("Munge8 elapsed:%d\n", elapsed);
    t->restart();
    delete t;
    elapsed=0;
    delete itab;
}

结果:

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076

Munge8 已过:1721468076


这里有断点:

    for (int i = 0; i < 10; i++) {
breakpoint >> int* itab=new int[1000*256]; //1MiB table 

Munge8 已过:1721528944

Munge8 已过:1721529048

Munge8 已过:1721529174

Munge8 已过:1721529281

Munge8 已过:1721529496

Munge8 已过:1721529554

Munge8 已过:1721529643

Munge8 已过:1721529756

Munge8 已过:1721529808

Munge8 已过:1721529896


有一个解决方案,但我仍然不明白为什么 boost::timer 会给我这样奇怪的结果。 可行的解决方案是使用来自&lt;time.h&gt;gettimeofday 函数。

class Timer {
private:

    timeval startTime;

public:

    void start(){
        gettimeofday(&startTime, NULL);
    }

    double stop(){
        timeval endTime;
        long seconds, useconds;
        double duration;

        gettimeofday(&endTime, NULL);

        seconds  = endTime.tv_sec  - startTime.tv_sec;
        useconds = endTime.tv_usec - startTime.tv_usec;

        duration = seconds + useconds/1000000.0;

        return duration;
    }

    long stop_useconds(){
        timeval endTime;
        long useconds;

        gettimeofday(&endTime, NULL);
        useconds = endTime.tv_usec - startTime.tv_usec;

        return useconds;
    }

    static void printTime(double duration){
        printf("%5.6f seconds\n", duration);
    }
};

测试:

//test

for (int i = 0; i < 10; i++) {
     void *vp = malloc(1024*sizeof(int));
     memset((int *)vp, 0, 1024);
    void* itab = malloc(sizeof(int)*1024*256); //1MiB table  
    if (itab) {
        memset ( (int*)itab, 0, 1024*256*sizeof (int) );
        float elapsed;

        boost::timer t;
        Timer timer = Timer();
        timer.start();

        Munge64(itab, 1024*256);

        double duration = timer.stop();
        long lt = timer.stop_useconds();
        timer.printTime(duration);
        cout << t.elapsed() << endl;
        elapsed = t.elapsed();
        cout << ios::fixed << setprecision(10) << elapsed << endl;
        cout << ios::fixed << setprecision(10) << t.elapsed() << endl;
        printf("Munge8 elapsed:%ld useconds\n", lt);

        elapsed = 0;
        free(vp);
        free(itab);
        //printf("Munge8 elapsed:%d\n", elapsed);
    }
}

结果:

0.000100 秒

0

40

40

Munge8 经过:100 微秒

0.000100 秒

0

40

40

Munge8 经过:100 微秒

0.000099 秒

0

40

40

Munge8 经过:99 微秒

【问题讨论】:

  • 也许您应该确保您要求printf 打印的内容是您实际传递的内容。 %d 预计不会翻倍。
  • 是的,但这不是每个结果都相同的原因
  • 实际上可以。 double 是 8 个字节,%d 占用其中的 4 个字节并将它们当作整数使用。对于不同的值,双精度部分的位表示可能相同,因为您只看到它的一半。正如您发现的那样,您的计时器可能没有为您提供所需的信息,但即使有,您的输出也不会接近您的预期。
  • 好的,好的,非常感谢

标签: c++ memory boost g++ alignment


【解决方案1】:

你不应该使用 boost::timer - http://www.boost.org/doc/libs/1_54_0/libs/timer/doc/original_timer.html#Class timer

在 POSIX 上,它测量 CPU 时间 - 而不是挂钟时间。

考虑使用 boost::chrono 或 std::chrono - 如果您想将自己与系统挂钟的漂移或偏移隔离开来,您可能希望在实现计时器时将 stable_clock 视为其他时钟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多