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,
source share