c/c++/OpenCV用于统计程序耗时的几种方法

本文介绍了三种在C/C++中计算程序耗时的方法:clock()函数、Windows下的GetTickCount()函数,以及OpenCV提供的getTickCount()和getTickFrequency()。通过实例演示了如何使用这些函数进行程序性能测量,并提到了GetTickCount在Linux环境的适用限制。
摘要由CSDN通过智能技术生成

c/c++用于计算程序耗时统计的几种方法

方法一 clock()函数

#include <stdio.h>        
#include <time.h>    
int main(const int argc,const char **argv)    
{
	long i = 10000000L;    
	clock_t start, end;    
    printf( "Time to do %ld empty loops is ", i );    
    start = clock();//start
    while( i-- );    
    end	 = clock();//end
    printf( "%f seconds\n", (double)(end - start) / CLOCKS_PER_SEC);    
    return 0;   
} 

更多关于time.h 相关的变量结构体函数使用可参考:飞机票✈ time.h

方法二 GetTickCount()函数

#include <stdio.h> 
#include <windows.h>
int main(const int argc,const char **argv)
{
	long i = 10000000L;
    DWORD start,end;
    printf( "Time to do %ld empty loops is ", i ); 
    start = GetTickCount();
	while( i-- );
    end = GetTickCount();
    printf( "%f seconds\n",(double)(start-end)/1000);
    return 0;
}

注意:windows.h,是windows 下的API接口,在linux下使用可能会报fatal error 的异常.

方法三 getTickCount()与getTickFrequency()函数

通常我们在做图片处理相关的业务时候需要对程序的性能做评估,OpenCV内部提供了两个函数用于计算程序的耗时,先看一个简单的demo

#include <stdio.h>
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
const string filename = "./lena.jpg";
int main(int argc, char **argv)
{
	int64 start=0,end=0;
	start = getTickCount();	//start
	Mat img,gray;
	img =imread(filename,1);
	cvtColor(img,gray,COLOR_BGR2GRAY);
	GaussianBlur(gray,gray,Size(7,7),1.5);//高斯滤波
	Canny(gray,gray,0,50);//边沿检测
	imshow("edges",gray);
	end = getTickCount(); //end
	printf("time: %f ms\n",1000.0*(end-start)/getTickFrequency());
	waitKey(0);

	return 0;
}

使用上述方法,必须要有OpenCV的开发环境,之余要怎么搭建OpenCV的开发环境,请同学自行百度解决。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的蕉尼基

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值