环境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;
}