Is Matlab still slower than opencv in C++

与世无争的帅哥 提交于 2019-12-05 12:52:45

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.

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

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).

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.

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.

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