c++统计执行时间,单位 s。
#include <opencv2/opencv.hpp>
double time0 = static_cast<double>(cv::getTickCount());
xxxxxx
time0 = ((double)cv::getTickCount() - time0) / cv::getTickFrequency();
std::cout << "time : " << time0 << std::endl;
C++版的getTickFrequency返回的是每秒钟的tick数
C版的cvGetTickFrequency返回的是每微妙的tick数
double t = (double)cvGetTickCount();
// 算法过程
t = (double)cvGetTickCount() - t;
printf( "run time = %gms\n", t/(cvGetTickFrequency()*1000) );
printf( "run time = %gs\n", t/(cvGetTickFrequency()*1000000) );
这//opencv 中如何进行程序的计时呢?
#include<opencv2/opencv.hpp>
using namespace cv;
#include<iostream>
using namespace std;
int main(void){
//查看opencv 读取一张图像所需要的时间
double time1 = static_cast<double>( getTickCount());//记录起始时间,其中getTickCount()函数返回CPU 自某个事件(如启动电脑)以来走过的时钟周期数
Mat img = imread("E:/testpicture/crystal.jpg");//通过绝对路径从指定文件夹 读取一张名字为 crystal的 图像,图像格式为 .jpg 注意读取图像必须指定格式
double time2 = (static_cast<double>( getTickCount()) - time1)/getTickFrequency();//获得了读取一张图像所需的时间单位为秒。 其中getTickFrequency()函数返回CPU一秒钟所走得时钟周期数。
cout<<"从文件中读取一张图像的时间是:"<< time2 <<"秒"<<endl;//输出运行时间
return 0;
}
int64 tStart = 0;
int64 tDuration = 0;
tStart = getTickCount();
tDuration = getTickCount() - tStart;
double fps = static_cast<double>(getTickFrequency() / tDuration);
int64 tStart = 0;
int64 tDuration = 0;
tStart = getTickCount();
tDuration = getTickCount() - tStart;
double fps = static_cast<double>(getTickFrequency() / tDuration);
printf("fps:%f\n",fps);
printf("fps:%f\n",fps);
计时函数
1. 纯C 高精度(100ns)测量慢
- #include <Windows.h>
- #include <stdio.h>
- int timer1_test()
- {
- LARGE_INTEGER start;
- LARGE_INTEGER end ;
- LARGE_INTEGER frequency;
- int i = 0;
- if (!QueryPerformanceFrequency(&frequency))
- {
- return -1;
- }
- QueryPerformanceCounter(&start); //开始计时
- // for (int i = 0; i < 100000; ++i)
- // {
- // ;// 用循环来测试计时
- // }
- Sleep(1);
- QueryPerformanceCounter(&end); //结束计时
- printf("main cost:%f\n", (double)(end.QuadPart - start.QuadPart) / (double)frequency.QuadPart); //打印for循环执行时间
- getchar();
- return 0;
- }
2.纯C 低精度(1ms)测量快
- #include <stdio.h>
- #include <time.h>
- void timer2_test()
- {
- clock_t start,end;
- start=clock();
- Sleep(1000);
- end=clock();
-
double duration = (double)(end - start); cout << "--> time: " << duration << " s" << endl; printf("time : %f ms \n",duration/1000);
double duration = (double)(end - start); cout << "--> time: " << duration << " s" << endl; printf("time : %f ms \n",duration/1000); - }
3.C++ OpenCV测试 高精度 view p copy
- #include"opencv.hpp"
- void timer3_test()
- {
- int64 start=0,end=0;
- start = getTickCount();
- Sleep(1000);
- end = getTickCount();
- cout << "The differences: " << 1000.0*(end - start)/getTickFrequency()<<" ms"<< endl;
- }
这个是在多线程下比较精确的
#include <time.h>
struct timespec time1={0,0};
struct timespec time2={0,0};
int main()
{
clock_gettime(CLOCK_REALTIME,&time1);
sRect result = tracker.update( frame);
clock_gettime(CLOCK_REALTIME,&time2);
///
sum_time += (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000 ;
printf("time : %d ms \n", (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000);
return 0;
}
<time.h>
struct timespec time1={0,0};
struct timespec time2={0,0};
int main()
{
clock_gettime(CLOCK_REALTIME,&time1);
sRect result = tracker.update( frame);
clock_gettime(CLOCK_REALTIME,&time2);
///
sum_time += (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000 ;
printf("time : %d ms \n", (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000);
return 0;
}