/*
* 本代码执行的平台是VS2015+OpenCV3.1+VC14 x32编译器
* CPU: Intel(R) Core(TM) i5-5200U CPU @2.20Ghz
* RAM: 8GB
*
* 测试图片分辨率为:1212 * 605
*
* 某个时刻Debug测试结果为:
* Return:1.0174e+07
* NotReturn:8.10438e+06
* Ratio: 1.25537
*
* 某个时刻Release测试结果为:
* Return:6.23904e+06
* NotReturn:4.55313e+06
* Ratio: 1.37028
*
* 总结: 实验操作代码必须可以对Mat的数据(data, 不是引用)进行操作,
* 如果不是操作只是简单的返回, 那么可能会由于优化, 达不到预期的效果.
* 如果只是简单的返回, 那么return速度要比not-return速度要快10%左右.即Ratio值大约是0.9
*/
#include <iostream>
#include <string>
#include "opencv2/opencv.hpp"
static cv::Mat ReturnMat()
{
std::string path = "../Resources/1-SRC.bmp";
cv::Mat img = cv::imread(path, cv::IMREAD_COLOR);
cv::Mat t;
t = img + 1;
return t;
}
static void NotReturnMat(cv::Mat& dst)
{
std::string path = "../Resources/1-SRC.bmp";
cv::Mat img = cv::imread(path, cv::IMREAD_COLOR);
dst = img + 1;
}
void test_return_Mat()
{
const int TIMES = 500;
// test return
double returnStart = (double)cv::getTickCount();
cv::Mat dst;
for (int i = 0; i < TIMES; ++i)
{
dst = ReturnMat();
}
double returnEnd = (double)cv::getTickCount();
double returnTime = returnEnd - returnStart;
std::cout << "Return:" << returnTime << std::endl;
// test not return
double notReturnStart = (double)cv::getTickCount();
cv::Mat dst2;
for (int i = 0; i < TIMES; ++i)
{
NotReturnMat(dst2);
}
double notReturnEnd = (double)cv::getTickCount();
double notReturnTime = notReturnEnd - notReturnStart;
std::cout << "NotReturn:" << notReturnTime << std::endl;
std::cout << "Ratio:\t" << double(returnTime) / double(notReturnTime) << std::endl;
}
基于OpenCV的函数返回Mat与通过引用获取参数的效率对比
最新推荐文章于 2023-01-03 17:35:42 发布