Matlab FFT vs MEX FFTW

12 views (last 30 days)
Justin
Justin on 8 Mar 2013
Answered: Friedrich on 7 Feb 2018
I recently wrote a simple routine in Matlab that uses an FFT in a for-loop; the FFT dominates the calculations. I wrote the same routine in mex just for experimentation purposes and it calls the FFTW library. It turns out that the matlab routine runs faster than the mex routine for very large arrays (about twice as fast). The mex routine uses wisdom and and performs the same FFT calculations. I also know matlab uses FFTW, but is it possible their version is slightly more optimized? I even used the FFTW_EXHAUSTIVE flag and its still about twice as slow for large arrays than the MATLAB counterpart. Furthermore I ensured the matlab I used was single threaded with the "-singleCompThread" flag and the mex file I used was not in debug mode. Just curious if this was the case - or if there are some optimizations matlab is using under the hood that I dont know about. Thanks.

Answers (2)

John
John on 15 Mar 2013
I was doing the same analysis comparing Matlab's built in function with a variety of FFT algorithms some of which I wrote. It turns out that Matlab FFT uses FFTW as you mentioned which is compiled C/C++ source code. It is highly optimized for large vectors > 1024. It comes down to optimal/adaptive execution based on array sizes.
It is definitely something under the hood.

Friedrich
Friedrich on 7 Feb 2018
The difference in speed might be caused by your inputs being rational and not complex. In this case Matlab calls the fft for real inputs, which is about twice as fast. On my machine this results in:
>> data = 1:2^24;
>> tic;fft(data);toc
Elapsed time is 0.602710 seconds.
>> data(1) = 1i;
>> tic;fft(data);toc
Elapsed time is 1.115792 seconds.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!