4

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,

smttsp
  • 4,011
  • 3
  • 33
  • 62
  • OpenCV uses several optimized libraries to speed up computation, so the question is: *were your OpenCV binaries compiled to use them?* Otherwise, Matlab will probably have the upper hand. – karlphillip Jul 16 '14 at 00:26
  • Also, one more important thing. All the values in MATLAB are floating points where as OpenCV has uchar, short, int, float, double. So that's why OpenCV comes out to be faster because most of the functions are carried on 8-bit data. – Froyo Jul 18 '14 at 08:00

5 Answers5

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

Niels
  • 81
  • 7
  • When I read the article, I couldn't be able to do proper comparison with c++ because it just indicates the speed ups using different packages and I don't know what is the `execution time` or `megaflops` of c++. Also, this article is published in 2000, so we don't know if the packages are improved lately. – smttsp Jul 15 '14 at 15:11
  • I think the speed of your code depends just as much on the compiler that compiles the code as on the library/language. Some of my friends who does a lot of scientific programming say they get a lot of extra speed just by compiling it with Intels compiler instead of the standard compiler in Visual Studio. In regard to Matlab (LAPACK or what ever Matrix library) vs OpenCV i dont think that the speed of libraries for doing BLAS have changed much over the years but when it comes to mores advanced stuff than CORR open cv might be faster. – Niels Jul 15 '14 at 15:26
  • Then, the speed comparison in the first website is wrong? At least not `2/10` vs `9/10`, is it? – smttsp Jul 15 '14 at 19:32
  • I would not give 2/10 vs 9/10 :) For instance in C++ it is very clear when you pass by reference and when you pass by value where as in Matlab it is not nearly as clear. It has been a while since a last did any real programming in Matlab but Mathworks might have improved this since I last used it. So it might be that for the specific code and problem they used in your link C++ was faster. Anyway the speed of matlab depends a lot on how you you write your code try for instance the code example given by Blaz Bratanic. – Niels Jul 16 '14 at 06:20
  • Just read your link again: "In OpenCV, we would get at least 30fps, resulting in real-time detection". If they are doing real time procecing from a live video feed and passing a lot of data around I think it is not unlikely that they get higher speed from openCV (C++) as their code does not just depends on how fast they can do basic matrix calculation. – Niels Jul 16 '14 at 06:31
2

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.

Zaw Lin
  • 5,629
  • 1
  • 23
  • 41
  • Is there anyway to improve the speed of opencv other than buying the `MKL`? If I find/get MKL, will my opencv code be as fast as matlab? – smttsp Jul 15 '14 at 19:28
  • I don't believe opencv has direct support for mkl. However, you can use math library such as Armadillo which can use mkl. Then you try to do your computation there. Openblas is an alternative to mkl. Depending on your problem, you might be able to get comparable speed with matlab. You would need to try running on your data and benchmark. – Zaw Lin Jul 15 '14 at 19:48
  • here's a bit more info http://nghiaho.com/?p=1726. so you can get some rough idea on the relative differences in performance. but these kind of benchmarks are very problem specific. so you really need to try out. – Zaw Lin Jul 15 '14 at 20:04
  • Is there any way to disable mkl in matlab? Then, I could be able to test how much mkl improves. Thanks for the link, it gives some idea about their speeds. – smttsp Jul 16 '14 at 05:31
  • I don't think that's possible since it's the fundamental workhorse to perform the computation. You might want to look into compiling opencv with ipp, with which you might be able to get similar performance, if opencv implemented ipp variant of `matchTemplate` function. – Zaw Lin Jul 16 '14 at 06:02
  • @smttsp Hi, did you find the way to disable mkl in matlab? – olivia May 02 '16 at 04:19
1

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

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • I was thinking, in the worst case, they would be in same speed but matlab's speed is double of opencv's speed. Why do you think opencv is so slow (or matlab is so fast) in this scenario. – smttsp Jul 15 '14 at 19:06
0

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.

Blaz Bratanic
  • 2,279
  • 12
  • 17
  • Loops have gotten much faster in MATLAB thanks to Just-in-Time compilation.. In fact for such a basic operation, loops can even be faster than vectorized MATLAB code. Try it yourself: http://pastebin.com/Ugca2aDx – Amro Jul 16 '14 at 16:06
  • btw if you change the order of the iterations (loop over `n` first, then `m`) it would be even faster. The reason is that MATLAB arrays are stored in [column-major order](https://en.wikipedia.org/wiki/Row-major_order#Column-major_order), so you'd be accessing the elements linearly that way (think [spatial locality](https://en.wikipedia.org/wiki/Locality_of_reference)). – Amro Jul 17 '14 at 11:53
0

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.

TripleS
  • 1,216
  • 1
  • 23
  • 39