计算程序执行时间

12 篇文章 1 订阅

计算程序执行耗时

  • 返回CPU执行的时间周期数,
cv.getTickCount,
  • 每秒CPU时间周期总数
cv.getTickFrequency

计算一段算法处理执行的时间秒数,代码结构如下:

e1 = cv.getTickCount()// your code executione2 = cv.getTickCount()time = (e2 - e1)/ cv.getTickFrequency()

time是以秒为单位。

计算秒/毫秒/FPS

在算法执行阶段,有三个经常使用的速度性能计量指标分别是秒、毫秒、FPS(每秒多少帧),根据上面的cv.getTickCount与cv.getTickFrequency两个函数,这三个指标的计算代码如下:

e1 = cv.getTickCount()
// your code execution
e2 = cv.getTickCount()

// 计算秒
time = (e2 - e1)/ cv.getTickFrequency()

// 计算毫秒
time2 =( (e2 - e1)/ cv.getTickFrequency() )*1000

// 计算FPS(每秒多少帧)
fps =( cv.getTickFrequency() / (e2 - e1) )



通过网上查阅资料,找到以下几种VC中求取程序运行时间的方法:
方法一 利用GetTickCount函数(ms)

//代码:
CString str;         
#include "windows.h"
double t1 = GetTickCount();//程序段开始前取得系统运行时间(ms)            
。。。。。。//to do sth
。。。。。。//to do sth
。。。。。。//to do sth
double t2 = GetTickCount();//程序段结束后取得系统运行时间(ms)         
double t = t2-t1;//程序运行时间(ms)   
str.Format("time:%f ms",t);//程序运行时间         

AfxMessageBox(str); 

另外一种:(这个比上面的稍微准确一点)

double ta = getTickCount();
。。。。。。//to do sth
。。。。。。//to do sth
。。。。。。//to do sth
Sleep(1000);
double tb = getTickCount();
cout << "tb-ta = " << 1000*(tb-ta)/getTickFrequency() << "ms" << endl;
 


 
方法二 利用C/C++计时函数(s)

//代码:
#include "time.h"

clock_t  start, finish;
start = clock(); 
。。。。。。//to do sth
。。。。。。//to do sth
。。。。。。//to do sth
finish = clock();
printf("%f seconds\n",(double)(finish-start)/CLOCKS_PER_SEC);


//VC/MFC中计算程序运行时间
方法三  利用CTime类 获取系统时间

//代码:
CString str;

//获取系统时间
CTime tm;
tm=CTime::GetCurrentTime();
str=tm.Format("现在时间是%Y年%m月%d日  %X");
AfxMessageBox(str);

方法四  利用GetLocalTime类 获取系统时间

SYSTEMTIME st;

CString strDate,strTime;

GetLocalTime(&st);

strDate.Format("M----",st.wYear,st.wMonth,st.wDay);

strTime.Format("-:-:-",st.wHour,st.wMinute,st.wSecond);

AfxMessageBox(strDate);

AfxMessageBox(strTime);


方法五 

//算法运行时间
int main()
{
    Mat img;
    int i, j;
    double t,t1;
    uchar* data;
    int imgSize;

    img = imread("lena_gray.jpg", 0);

    t = (double)cvGetTickCount();//
    for (j = 0; j < img.rows; j++)
    {
        for (i = 0; i < img.cols; i++)
        {
            img.at<uchar>(j, i) = 255;
        }
    }
    t1 = cvGetTickCount();
    t = (t1-t)/(cvGetTickFrequency()*1000);//
    cout << "at operation cost : " << t << "ms" << endl;//

    data = img.ptr<uchar>(0);
    imgSize = img.rows*img.cols*img.channels();
    t = cvGetTickCount();
    for (j = 0; j < imgSize; j++)
    {
        data[j] = 255;
    }
    t1 = (double)cvGetTickCount();
    t = (t1-t)/(cvGetTickFrequency()*1000);
    cout << "ptr operation cost : " << t << "ms" << endl;

    return 0;
}


经过计算,对512*512的图像进行编历,
at操作耗时为0.424076ms
ptr操作耗时为0.0346413ms

double duration;
duration = static_cast<double>(cv::getTickCount());
。。。。。。//to do sth
。。。。。。//to do sth
。。。。。。//to do sth
duration = static_cast<double>(cv::getTickCount())-duration;
duration /= cv::getTickFrequency(); // the elapsed time in ms

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值