C++ 程序耗時統計

環境ubuntu16.04,c++ time.h裏面的定時器也太坑了,還沒我的手標準

 

這裏因爲做cv,所以很方便使用opencv自導的定時器,稍微封裝了一下,硬起來更加方便。


#include <stdio.h>
#include <string>
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

class AaronTimer {

public:
//    explicit AaronTimer() {m_startTickCount = static_cast<double>(cv::getTickCount());};
//    virtual ~AaronTimer() {};
    double m_startTickCount = 0.0;
    double m_endTickCount = 0.0;
    double m_timeSecond = 0.0;
    double m_timeMs = 0.0;

    void start() {m_startTickCount = static_cast<double>(cv::getTickCount());}
    void stop() {
        m_endTickCount = static_cast<double>(cv::getTickCount());
        m_timeSecond = (m_endTickCount - m_startTickCount) / cv::getTickFrequency();
        m_timeMs = m_timeSecond*1000;
    }
};

void main(){
    AaronTimer timer;
    timer.start();
    int i = 1000000;
    while(i > 0) {
        i--;
    }
    tmier.stop();
    std::cout << "using time = " << timer.m_timeMs << " ms." << std::endl;
}

 

當然還是順道把系統的也貼出來,有時候沒有opencv可以勉強使用


#include "time.h"

void main(){

    clock_t startTime, endTime, videoWisedStartTime, videoWisedEndTime;
    startTime = clock();
    /////////程序.....
    endTime = clock();
    double time = (double) (endTime - startTime) / CLOCKS_PER_SEC * 1000;
    std::cout << "process one pic use: " << time << " ms." << std::endl;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章