6

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,

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
smttsp
  • 4,011
  • 3
  • 33
  • 62
  • Compilers are still so s####y they can't remove a simple for loop that always returns the same values? Where are my flying cars! :-) – xanatos Sep 03 '13 at 13:20
  • use `mex -v` to compile the mex and see what flags matlab is using. – Shai Sep 03 '13 at 13:20
  • 6
    are you compiling in debug or release mode in VS? – Shai Sep 03 '13 at 13:20
  • what are the type changes you are making between C++ and mex? – Shai Sep 03 '13 at 13:21
  • 4
    The compiler options are probably different. Different levels of optimization would definitely explain the difference. Try `mex -v yourfile.cpp` to examine the command line options used by mex, and compare them to the command line produced by your C++ IDE. – Peter Sep 03 '13 at 13:21
  • @Shai: it is Debug mode. I'm adding Matlab code too – smttsp Sep 03 '13 at 13:23
  • 1
    @smttsp use release mode, and only then compare performance. – Shai Sep 03 '13 at 13:53
  • 1
    @Shai: As you suggest changing it to release mode made a big difference – smttsp Sep 03 '13 at 14:04
  • @smttsp debug mode (as its name suggests) is only for debug. If you want to measure performance use release mode. always. – Shai Sep 03 '13 at 14:06
  • I didnt know that. I'm reading about release vs debug mode now – smttsp Sep 03 '13 at 14:09

2 Answers2

3

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.

pattivacek
  • 5,617
  • 5
  • 48
  • 62
  • I changed optimizations but some of them give error, saying that "Command line error D8016: '/ZI' and '/Ob1' command-line options are incompatible". – smttsp Sep 03 '13 at 13:49
  • Yeah, it is because of release/debug mode. When I change debug to release it becomes `920 ms` which is almost same as matlab running time – smttsp Sep 03 '13 at 13:50
  • For code that simple, I think it is reasonable that the two compilers would yield output with similar performance, although there is still plenty of room for variation due to other parameters. – pattivacek Sep 03 '13 at 13:53
  • What do you mean by "plenty of room for variation"? Could you explain a bit more? – smttsp Sep 03 '13 at 14:00
  • My post describes many reasons that there could still be variation in compiling your code, from external library dependencies to compilation flags to the simple fact that the VS2010 compiler might just not be very good. – pattivacek Sep 03 '13 at 14:09
3

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

You leave them both unininialized; see this question as to why that is bad.

MEX files are by default in ANSI C. Your code indeed looks like that. Double-check your mex -setup; you might have accidentally selected a C-compiler, thinking you were selecting a C++ one.

Also make sure that you have the exact same set of compiler options for both compiles. Exactly the same.

But I think the core of the matter is that you are doing integer arithmetic in the C++ version and double arithmetic on the MATLAB version. This could make a substantial difference.

Other than that 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 rather trivial, and remove it altogether.

Community
  • 1
  • 1
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • It is not related to `mex -setup`. There is slight difference between c++ and matlab after changing it to release mode but this is acceptable compared to being 3.5 times slower. – smttsp Sep 03 '13 at 14:02
  • @smttsp: What do you find "acceptable"? There *should* be no difference at all. – Rody Oldenhuis Sep 03 '13 at 14:09
  • Being 1-10% slower than matlab is acceptable because this might be due to the state of the machine, other programs etc. But being 3.5 times slower is totally unacceptable. – smttsp Sep 03 '13 at 17:54