【发布时间】:2016-06-08 10:16:13
【问题描述】:
我正在使用内部函数来加速正在运行的 openCV 代码。但是在我用 Intrinsics 替换代码之后,代码的运行时成本几乎相同,甚至可能更糟。我无法弄清楚这是什么以及为什么会发生这种情况。我一直在寻找这个问题很长一段时间,但注意到变化。如果有人可以帮助我,我们将不胜感激。非常感谢你!这是我的代码
// if useSSE is true,run the code with intrinsics and takes 1.45ms in my computer
// and if not run the general code and takes the same time.
cv::Mat<float> results(shape.rows,2);
if (useSSE) {
float* pshape = (float*)shape.data;
results = shape.clone();
float* presults = (float*)results.data;
// use SSE
__m128 xyxy_center = _mm_set_ps(bbox.center_y, bbox.center_x, bbox.center_y, bbox.center_x);
float bbox_width = bbox.width/2;
float bbox_height = bbox.height/2;
__m128 xyxy_size = _mm_set_ps(bbox_height, bbox_width, bbox_height, bbox_width);
gettimeofday(&start, NULL); // this is for counting time
int shape_size = shape.rows*shape.cols;
for (int i=0; i<shape_size; i +=4) {
__m128 a = _mm_loadu_ps(pshape+i);
__m128 result = _mm_div_ps(_mm_sub_ps(a, xyxy_center), xyxy_size);
_mm_storeu_ps(presults+i, result);
}
}else {
//SSE TO BE DONE
for (int i = 0; i < shape.rows; i++){
results(i, 0) = (shape(i, 0) - bbox.center_x) / (bbox.width / 2.0);
results(i, 1) = (shape(i, 1) - bbox.center_y) / (bbox.height / 2.0);
}
}
gettimeofday(&end, NULL);
diff = 1000000*(end.tv_sec-start.tv_sec)+end.tv_sec-start.tv_usec;
std::cout<<diff<<"-----"<<std::endl;
return results;
【问题讨论】:
-
一个工作代码可以帮助你得到一些答案。请看如何做一个minimal reproducible example
-
另外,你应该描述你的代码做什么。
-
你真的需要
div_ps还是可以乘以倒数? -
哪个编译器?例如,如果您使用的是 Windows 和 VS2012 或更高版本,您可能会发现这些简单的
for循环是automatically vectorized。 -
调用 gettimeofday 可能会使其他一切相形见绌。您应该考虑将时间从函数体中提升出来,并改为对它进行 1000000 次调用。此外,您似乎没有调整自己的价值观。
标签: c++ opencv sse intrinsics