C++ 为什么boost::timer会出现如此奇怪的结果?

C++ 为什么boost::timer会出现如此奇怪的结果?,c++,linux,boost,time,g++,C++,Linux,Boost,Time,G++,为什么boost::timer会给我如此奇怪的结果? 我的工作解决方案是使用中的关于gettimeofday函数的包装器,但我不明白为什么boost::timer在这里对我不起作用。我做错了什么 class Timer { private: timeval startTime; public: void start(){ gettimeofday(&startTime, NULL); } double stop(){

为什么boost::timer会给我如此奇怪的结果? 我的工作解决方案是使用
中的关于
gettimeofday
函数的包装器,但我不明白为什么
boost::timer
在这里对我不起作用。我做错了什么

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);
    }
};
测试:

//测试
对于(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表
国际单项体育联合会(itab){
memset((int*)itab,0,1024*256*sizeof(int));
浮子过去了;
boost::定时器t;
定时器=定时器();
timer.start();
Munge64(itab,1024*256);
双持续时间=timer.stop();
long lt=计时器。停止使用秒();
计时器。打印时间(持续时间);

cout您不应该使用boost::timer-timer

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


考虑使用boost::chrono或std::chrono——在实现计时器时,如果您想将自己与系统挂钟中的漂移或移位隔离开来,您可能会希望使用稳定时钟——就像其他时钟一样。我希望在POSIX上,这将使用时钟上的时间单调。

谢谢,我错过了现在不推荐使用的时钟,因为我正在查看过时的类引用
//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);
    }
}