for循环的一种加速方法
2017-02-20 22:24
295 查看
原文:http://blog.csdn.net/CXP2205455256/article/details/42044107
一、源码如下:
[cpp] view
plain copy
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <istream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Mat srcImg = imread("D:\\Documents\\Desktop\\图像拼接测试图片\\11-over750.bmp", CV_LOAD_IMAGE_GRAYSCALE);
Mat dstImg1, dstImg2;
srcImg.copyTo(dstImg1);
srcImg.copyTo(dstImg2);
uchar *srcData = srcImg.data;
uchar *dstData1 = dstImg1.data;
uchar *dstData2 = dstImg2.data;
int size = srcImg.cols * srcImg.rows;
int threshold = 122;
int i;
double ts1 = ( double )getTickCount();
for(i = 0; i < size; ++i)
{
if(srcData[i] < threshold)
dstData1[i] = 0;
else
dstData1[i] = 255;
}//i
double te1 = ( double )getTickCount();
double tim1 = (te1 - ts1) * 1000.0 / getTickFrequency();
double ts2 = ( double )getTickCount();
i = 0;
int size2 = size - 4;
for(; i < size2; i += 4)
{
if(srcData[i] < threshold)
dstData2[i] = 0;
else
dstData2[i] = 255;
if(srcData[i + 1] < threshold)
dstData2[i + 1] = 0;
else
dstData2[i + 1] = 255;
if(srcData[i + 2] < threshold)
dstData2[i + 2] = 0;
else
dstData2[i + 2] = 255;
if(srcData[i + 3] < threshold)
dstData2[i + 3] = 0;
else
dstData2[i + 3] = 255;
}//i
for(; i < size; ++i)
{
if(srcData[i] < threshold)
dstData2[i] = 0;
else
dstData2[i] = 255;
}//i
double te2 = ( double )getTickCount();
double tim2 = (te2 - ts2) * 1000.0 / getTickFrequency();
cout << " one loop:" << tim1 << endl;
cout << "one four loop:" << tim2 << endl;
namedWindow("img1");
namedWindow("img2");
imshow("img1", dstImg1);
imshow("img2", dstImg2);
waitKey(0);
return 0;
}
时间对比:
1、Debug下时间:
2、Release下时间:
结论:几乎加速一倍。
一、源码如下:
[cpp] view
plain copy
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <istream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Mat srcImg = imread("D:\\Documents\\Desktop\\图像拼接测试图片\\11-over750.bmp", CV_LOAD_IMAGE_GRAYSCALE);
Mat dstImg1, dstImg2;
srcImg.copyTo(dstImg1);
srcImg.copyTo(dstImg2);
uchar *srcData = srcImg.data;
uchar *dstData1 = dstImg1.data;
uchar *dstData2 = dstImg2.data;
int size = srcImg.cols * srcImg.rows;
int threshold = 122;
int i;
double ts1 = ( double )getTickCount();
for(i = 0; i < size; ++i)
{
if(srcData[i] < threshold)
dstData1[i] = 0;
else
dstData1[i] = 255;
}//i
double te1 = ( double )getTickCount();
double tim1 = (te1 - ts1) * 1000.0 / getTickFrequency();
double ts2 = ( double )getTickCount();
i = 0;
int size2 = size - 4;
for(; i < size2; i += 4)
{
if(srcData[i] < threshold)
dstData2[i] = 0;
else
dstData2[i] = 255;
if(srcData[i + 1] < threshold)
dstData2[i + 1] = 0;
else
dstData2[i + 1] = 255;
if(srcData[i + 2] < threshold)
dstData2[i + 2] = 0;
else
dstData2[i + 2] = 255;
if(srcData[i + 3] < threshold)
dstData2[i + 3] = 0;
else
dstData2[i + 3] = 255;
}//i
for(; i < size; ++i)
{
if(srcData[i] < threshold)
dstData2[i] = 0;
else
dstData2[i] = 255;
}//i
double te2 = ( double )getTickCount();
double tim2 = (te2 - ts2) * 1000.0 / getTickFrequency();
cout << " one loop:" << tim1 << endl;
cout << "one four loop:" << tim2 << endl;
namedWindow("img1");
namedWindow("img2");
imshow("img1", dstImg1);
imshow("img2", dstImg2);
waitKey(0);
return 0;
}
时间对比:
1、Debug下时间:
2、Release下时间:
结论:几乎加速一倍。
相关文章推荐
- for循环的一种加速方法
- for双层循环的一种使用方法:Java新用法
- 在上JAVA课时看到了一种的调试方法,帮我把这个循环弄明白了,而且对FOR循环的理解也更深了
- python中for循环输出列表索引与对应的值方法
- 一个技术小白整理出来的for循环使用几种方法~~
- [论文学习]An Effective Approach for Mining Mobile User Habits:一种高效挖掘移动用户习惯的方法
- 一种方便的方法for android studio 被墙安装失败。
- for循环退出方法,带冒号:的退出等
- 解决linux下WaitForMultipleObjects的一种方法
- 结合网上内容,分享一个,java用for循环创建i个对象,的方法
- 一种利用线程池进行for循环处理的思想
- C#分别使用for,while,do-while循环方法求的n!
- Lua语言教程1 ——【变量输出,方法输出,if语句和for循环语句!】
- 请教嵌套for循环对比优化方法
- JavaScript里的循环方法:forEach,for-in,for-of
- for...in和for...of循环以及forEach方法
- php for 循环语句使用方法详细说明
- 循环不变性(loop invariant)-证明算法的正确性的一种方法
- 一种基于ARM平台的Android启动加速方法
- java的几种for循环方法