Matlab vs C ++ speed comparison in this code

I wrote simple C ++ code and tested it in C ++, then I adapted the same code for MATLAB to mex file_name.cpp and ran the same code in MATLAB that uses 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 the 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 edited this code for MATLAB, i.e. changing the appropriate types, adding a MEX function, etc., and the results are about 900 ms in MATLAB, and not 3100 ms in C ++.

What I donโ€™t understand is the same as the same code and the same compiler (in MATLAB I write mex -setup on the command line and the selected Visual Studio compiler as the MEX compiler), however MATLAB is around B 3. 5 times faster .

What makes MATLAB faster and what does C ++ not? Can someone explain to me why there is such a huge difference? I tried some other codes, all 3-6 times faster in MATLAB.

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

Is this possible due to my version of Visual Studio? If I change it to VS2012, will it be faster?

the output of mex -v is here.

Thanks,

+6
source share
2 answers

Performance is highly dependent on the platform, OS, compiler, etc. Regardless of what Matlab does in this case, it somehow managed to find an optimization that the VS2010 compiler did not. I would venture to suggest that upgrading to VS2012 would not be significant, but I could be wrong. This is, after all, a different compiler.

I admit this is somewhat surprising, but check the compilation flags and try profiling with different configurations. If your Matlab installation is 32-bit, this may also matter.

There may also be slight differences in your code, perhaps light enough that you do not notice. Your code may be linked to other libraries, which may also have large performance differences.

The lesson here is that it is very difficult to pinpoint why one thing works better than another.

EDIT: You mentioned that the code was compiled for debugging. This only further increases the variation of what the compilers will output, since enabling debugging options can also disable other optimizations, and each compiler has a different idea of โ€‹โ€‹what debugging information is important and whether it is in your code.

I would recommend disabling all debugging options to get more consistent output. I would also recommend that you compile with similar levels of optimization, perhaps maximally or not at all.

+3
source

In C ++ code, you use int k in the inner loop, while in MATLAB code you use double a (and, oddly, change the a++ notation to a=a+1 ...)

You leave them both initialized, see this question as to why this is bad.

MEX files are used by default in ANSI C. Your code really looks like this. Double check your mex -setup ; you may have accidentally chosen a C compiler, considering that you are choosing C ++.

Also make sure that you have the same set of compiler options for both compilers. Similar.

But I think the bottom line is that you are doing integer arithmetic in the C ++ version and double arithmetic in the MATLAB version. This can be significant.

Apart from this and what has already been mentioned here, there should be no difference. In fact, any decent compiler with even basic optimizations should be able to detect that this loop is pretty trivial and completely removes it.

+3
source

Source: https://habr.com/ru/post/953060/


All Articles