MATLAB Coder does not generally speed up MATLAB built-in functions like EIG, SVD, FFT, FFT2, FFTN, QR, LU, etc. In fact, the correct expectation is that MATLAB Coder generated MEX functions that are dominated by calculations with MATLAB built-in functions like these will be slightly slower than equivalent code running in MATLAB. This is because the MATLAB functions are compiled already, and sometimes they are even multi-threaded.
Specifically the FFT function in MATLAB (and thus the FFT2 function) is highly optimized for the PC. The generated C code on the other hand is more of a readable & portable C code and is not optimized for a particular platform. So the slowdown in this case is expected regardless of MEX or standalone compilation. As a general note, compiled code is not always faster than MATLAB and FFT is one of those cases.
However, starting in R2016a and R2017b, MATLAB Coder added the ability for the generated code to call LAPACK and FFTW respectively:
Using these will give you optimized linear algebra and FFT routines respectively that are near the performance of MATLAB.
For releases prior to R2016a, here are some options you can try for improving performance:
1) Turn off debugging for the compiler to enable optimizations
2) Turn off array bounds checking and Ctrl+C checking
Make sure you have turned off the Ensure Memory Integrity option, and probably also the Enable Responsiveness checks. This usually makes a huge difference. The integrity checks prevent the C compiler from generating efficient code for computationally-intensive loops.
The link below illustrates how to control the runtime checks in MATLAB coder:
3) Before R2017b, MATLAB Coder generated code for a simple radix-2 FFT algorithm. MATLAB uses FFTW. For large data (even moderately sized, actually), FFTW performs better than radix-2 FFT. To use FFTW before R2017b, you will need to make your FFT function extrinsic by adding the following line to your MATLAB code:
When running generated code in the MATLAB environment, calls to extrinsic functions transfer control from the generated code to MATLAB. MATLAB Coder does not compile or generate code for extrinsic functions. The overhead associated with data copying may be a bottleneck, but since you are running this code as a MEX file, 'coder.extrinsic' for the FFT2 function may help.
There is a section in our Help Documentation on accelerating MATLAB algorithms with MATLAB Coder. This link can be a useful reference for such situations:
Best Answer