您的位置:首页 > 编程语言

测试矩阵连续运算的速度问题的代码

2016-09-22 11:15 671 查看
float r = 0.0f;
RNG rng(1);

double t1 = (double)getTickCount();
for (size_t i = 0; i < 10000; i++)
{
Mat a, b, c;
a = Mat::ones(512, 512, CV_32FC1);
a = a * rng.uniform(0.0f, 100.0f);
b = Mat::ones(512, 512, CV_32FC1);
b = b * rng.uniform(0.0f, 100.0f);
c = Mat::ones(512, 512, CV_32FC1);
c = c * rng.uniform(0.0f, 100.0f);
Mat d = (a.mul(b) + c)*1000.f;
int x = 511 * rng.uniform(0.0f, 1.0f);
int y = 511 * rng.uniform(0.0f, 1.0f);
r = r + d.at<float>(x, y);
}
t1 = (double)getTickCount() - t1;

std::cout << "compute time :" << t1*1000.0 / cv::getTickFrequency() << " ms \n";
cout << r << endl;
r = 0.0f;

double t2 = (double)getTickCount();
for (size_t i = 0; i < 10000; i++)
{
cv::Mat a, b, c;
a = Mat::ones(512, 512, CV_32FC1);
a = a * rng.uniform(0.0f, 100.0f);
b = Mat::ones(512, 512, CV_32FC1);
b = b * rng.uniform(0.0f, 100.0f);
c = Mat::ones(512, 512, CV_32FC1);
c = c * rng.uniform(0.0f, 100.0f);
Mat d = a.mul(b);
d = d + c;
d = d*1000.f;
int x = 511 * rng.uniform(0.0f, 1.0f);
int y = 511 * rng.uniform(0.0f, 1.0f);
r = r + d.at<float>(x, y);
}
t2 = (double)getTickCount() - t2;

std::cout << "compute time :" << t2*1000.0 / cv::getTickFrequency() << " ms \n";
cout << r << endl;
r = 0.0f;

double t3 = (double)getTickCount();
for (size_t i = 0; i < 10000; i++)
{
cv::Mat a, b, c;
a = Mat::ones(512, 512, CV_32FC1);
a = a * rng.uniform(0.0f, 100.0f);
b = Mat::ones(512, 512, CV_32FC1);
b = b * rng.uniform(0.0f, 100.0f);
c = Mat::ones(512, 512, CV_32FC1);
c = c * rng.uniform(0.0f, 100.0f);
Mat d = a.mul(b);
d += c;
d *= 1000.f;
int x = 511 * rng.uniform(0.0f, 1.0f);
int y = 511 * rng.uniform(0.0f, 1.0f);
r = r + d.at<float>(x, y);
}
t3 = (double)getTickCount() - t3;

std::cout << "compute time :" << t3*1000.0 / cv::getTickFrequency() << " ms \n";
cout << r << endl;
上面的计算复杂度是一样的。
实验证明,第二种方式最快,矩阵表达式计算要尽量展开,并且不要使用*=或+=等简化编程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: