MATLAB Coder: C Version Much Slower Than Mex Version
I used MATLAB Coder to turn my MATLAB code into C code and expected a great decrease in run time, but instead found that my C code runs much slower than both the MATLAB code and the mex code. Over 5 tests, I get the following:
Tthe MATLAB code took 77.48 seconds on average.
The mex version running in MATLAB took 46.27 seconds on average.
The generated C code took 262.35 seconds on average.
All three versions of the code produces the exact same output, so I am confident that each version is working properly. I am running the MATLAB and mex versions in R2023a and the C version in Debian 11 (bullseye) in a docker container. The cmake command I am running is
cmake -DCMAKE_BUILD_TYPE=Release ..
and my understanding is that this enables the C compiler optimisations, but I’m unsure if there are further optimisations I can enable. Before specifying the build type as release, my C code ran about 90 seconds slower still (~350s), which is horrendous compared to the MATLAB/mex version.
These are the settings I am generating my code with:
The full settings I used are attached in config.mat but they are almost all just the default settings.
My understanding is that MATLAB does some computations in parallel automatically, while the generated C code doesn’t, so I tried turning on automatic parallelisation in the generated code. This only made the code run slower, however. I’m not sure how the automatic paralellisation works, but when I manually tried to parallelise parts of my code, it also did not run faster, so I did not really expect this to improve anything.
I’m just really unsure why the C version runs so much slower than even the mex version, which I expected to be similar or worse than the C version. Are there settings I can tweak in Coder? Are there further optimisations I can enable on the Linux side of things? If this is normal behaviour, what is the explanation for the discrepency between C and mex?I used MATLAB Coder to turn my MATLAB code into C code and expected a great decrease in run time, but instead found that my C code runs much slower than both the MATLAB code and the mex code. Over 5 tests, I get the following:
Tthe MATLAB code took 77.48 seconds on average.
The mex version running in MATLAB took 46.27 seconds on average.
The generated C code took 262.35 seconds on average.
All three versions of the code produces the exact same output, so I am confident that each version is working properly. I am running the MATLAB and mex versions in R2023a and the C version in Debian 11 (bullseye) in a docker container. The cmake command I am running is
cmake -DCMAKE_BUILD_TYPE=Release ..
and my understanding is that this enables the C compiler optimisations, but I’m unsure if there are further optimisations I can enable. Before specifying the build type as release, my C code ran about 90 seconds slower still (~350s), which is horrendous compared to the MATLAB/mex version.
These are the settings I am generating my code with:
The full settings I used are attached in config.mat but they are almost all just the default settings.
My understanding is that MATLAB does some computations in parallel automatically, while the generated C code doesn’t, so I tried turning on automatic parallelisation in the generated code. This only made the code run slower, however. I’m not sure how the automatic paralellisation works, but when I manually tried to parallelise parts of my code, it also did not run faster, so I did not really expect this to improve anything.
I’m just really unsure why the C version runs so much slower than even the mex version, which I expected to be similar or worse than the C version. Are there settings I can tweak in Coder? Are there further optimisations I can enable on the Linux side of things? If this is normal behaviour, what is the explanation for the discrepency between C and mex? I used MATLAB Coder to turn my MATLAB code into C code and expected a great decrease in run time, but instead found that my C code runs much slower than both the MATLAB code and the mex code. Over 5 tests, I get the following:
Tthe MATLAB code took 77.48 seconds on average.
The mex version running in MATLAB took 46.27 seconds on average.
The generated C code took 262.35 seconds on average.
All three versions of the code produces the exact same output, so I am confident that each version is working properly. I am running the MATLAB and mex versions in R2023a and the C version in Debian 11 (bullseye) in a docker container. The cmake command I am running is
cmake -DCMAKE_BUILD_TYPE=Release ..
and my understanding is that this enables the C compiler optimisations, but I’m unsure if there are further optimisations I can enable. Before specifying the build type as release, my C code ran about 90 seconds slower still (~350s), which is horrendous compared to the MATLAB/mex version.
These are the settings I am generating my code with:
The full settings I used are attached in config.mat but they are almost all just the default settings.
My understanding is that MATLAB does some computations in parallel automatically, while the generated C code doesn’t, so I tried turning on automatic parallelisation in the generated code. This only made the code run slower, however. I’m not sure how the automatic paralellisation works, but when I manually tried to parallelise parts of my code, it also did not run faster, so I did not really expect this to improve anything.
I’m just really unsure why the C version runs so much slower than even the mex version, which I expected to be similar or worse than the C version. Are there settings I can tweak in Coder? Are there further optimisations I can enable on the Linux side of things? If this is normal behaviour, what is the explanation for the discrepency between C and mex? gcc, mex, optimisation, optimization, linux, cmake MATLAB Answers — New Questions