Is Matlab still slower than opencv in C++

这一生的挚爱 提交于 2019-12-07 10:56:57

问题


According to this link and this one, it is said that opencv is much faster than matlab. First link is written in March 2012, second one is a bit later than that.

In the first link, it says, "Programs written in OpenCV run much faster than similar programs written in Matlab." and rates Matlab: 2/10 and OpenCV: 9/10

Consider, I have two float Matrix whose size are 1024*1024(mat1 and mat2). I want to correlate this matrices.

In matlab,

corr2(mat1,mat2);     //70-75 ms

In opencv, c++

Mat result(1,1,CV_32F);
matchTemplate(mat1,mat2,result, CV_TM_CCOEFF_NORMED);      // 145-150 ms

As far as I know, c and c++ are in approximately same speed.

So, I wonder, why matlab is faster than opencv/c++ while doing cross correlation. Is it because I am comparing wrong things (even though the results are same) or is the cross correlation implementation of matlab is double faster than opencv implementation?

Note that, I'm using Matlab 2013a and Visual Studio 2010.

Thanks,


回答1:


Matlab built in functions comes with mkl and opencv's dont. So if two exactly equivalent functions are present in both, matlab is likely to be faster(much) than opencv. I have tried to do pseudo inverse on a large matrix and matlab beat everything(openblas,Armadillo,self integrated mkl etc) by at least 2 times. Then I just stopped figuring out why and just load the data into matlab and let it do the thing. opencv is by far the slowest. Try matrix multiplication on a 10000x10000 matrix in opencv. it took 10 minutes on my laptop. Matlab took 1 minute.




回答2:


Matlab is not as bad as you may think at doing matrix calculations. For many of the Basic Linear Algebra operation Matlab is calling rutines written in fortran and c++. So as long as you dont use loops and formulate it in matrix operations Matlab is actually very fast.

http://www.mathworks.se/company/newsletters/articles/matlab-incorporates-lapack.html




回答3:


In your scenario, there is no reason to expect matlab to be slower. You are calling a single function, the overhead caused by the language interpreter and passing the data to a native function (mex function) has to be paid only once.

If you would call the same function 1024 times for a small 32*32 matrices, you will probably notice the overhead (unless the JIT-Compiler finds a neat trick to optimize the code).




回答4:


Matlab can be fast if you vectorize everything and use native functions. But if you would do some operations in a loop i.e.

A = zeros(100,100);
for m = 1:100
    for n = 1:100
        A(m, n) = 1/(m + n - 1);
    end
end

vs.

Mat A(100, 100, CV_64F);
for (int r = 0; r < A.rows; r++)
  for (int c = 0; c < A.cols; c++) 
    A.at<double>(r, c) = 1 / (r + c - 1);

you would notice the difference.




回答5:


For correlation functions (and many more) matlab uses an advance libraries which uses an advanced instruction set.

However Matlab is smart than you think, Matlab Checks on runtime if the operation would execute faster on spatial domain or frequency domain, than execute fastest solution.

I couldn't find a mention for corr2, however I found for normxcorr2

Calculate cross-correlation in the spatial or the frequency domain, depending on size of images.



来源:https://stackoverflow.com/questions/24761170/is-matlab-still-slower-than-opencv-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!