Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab vs c++ speed comparison in this code

I have written simple C++ code and tested it in C++, then I have adapted the same code for MATLAB by mex file_name.cpp and run the same code in MATLAB which is using the same compiler as C++. Here is the code:

int k;
for(int j = 0; j < 100;j++){
    for(int i = 0; i < 10000000; i++){
        k++;
    }
    k/=10000000
}

Here is MATLAB code:

double a;int j;int i;
double* k;

for(j = 0; j < 100;j++){
    for(i = 0; i < 10000000; i++){
        a = a+1;
    }
    a = a / 10000000;
}

plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
k = mxGetPr(plhs[0]);
*k = (double)a;

I have edited this code for MATLAB, i.e changing to suitable types, adding MEX-function etc and the results are approx 900ms in MATLAB as opposed to 3100 ms in C++.

What I don't understand is both are running the same code and with the same compiler (in MATLAB I write mex -setup in command line and selected Visual Studio compiler as MEX compiler), however, MATLAB is around 3.5 times faster.

What is MATLAB doing to be that faster and what is C++ not doing? Could somebody please explain me why there is so huge difference? I have tried some other codes, all are 3-6 times faster in MATLAB.

My PC is 64-bit Windows 7, Visual Studio 2010 is used for C++, MATLAB is R2012b.

Is it possible this is because of my Visual Studio version? If I change it to VS2012, would it be faster?

mex -v output is here.

Thanks,

like image 805
smttsp Avatar asked Sep 03 '13 13:09

smttsp


People also ask

Which is faster MATLAB or C?

Performance is comparable for small to medium size systems while the C implementation is up to 2.5 times faster than MATLAB for large systems, which makes sense!

Is C better than MATLAB?

MATLAB is an interpreted language and C is a compiled language. MATLAB and C has different compilers. MATLAB has a rich built-in library of thousands of functions. C is efficient than MATLAB in loops ( for, while, do) .

Is MATLAB a fast language?

Matlab is between 9 to 11 times slower than the best C++ executable. R runs between 475 to 491 times slower than C++. If the code is compiled, the code is between 243 to 282 times slower. Hybrid programming and special approaches can deliver considerable speed ups.

Is MATLAB a slow language?

MATLAB is slow since it is an interpreted language that is MATLAB programs are not converted into Machine language but are run by external software, so it can sometimes be slow.


1 Answers

Performance is highly dependent on platform, OS, compiler, etc. Whatever Matlab is doing in this case, it somehow has managed to find an optimization that the VS2010 compiler did not. I would venture to guess that upgrading to VS2012 would not make a substantial difference, but I could be wrong. It is, after all, a different compiler.

I will admit that this is somewhat surprising, but check your compilation flags and try profiling with different configurations. If your Matlab install is 32-bit, that could make a difference, as well.

There could also be slight differences in your code, possibly slight enough that you might not have noticed. Your code might be linking against other libraries that could also have wide variation in performance.

The lesson here is that it can be very difficult to pin down exactly why one thing performs better than another.

EDIT: You have mentioned that the code is compiled for debugging. This only further increases the variation of what compilers will output, since activating debug options may also turn off other optimizations, and each compiler has a different idea of what sort of debug information is important and worth sticking in your code.

I would recommend turning off all debug options to get a more consistent output. I would also recommend making sure you are compiling with similar levels of optimization, probably either the greatest possible or not at all.

like image 97
pattivacek Avatar answered Oct 02 '22 20:10

pattivacek