MATLAB Answers

0

Matlab FFT vs MEX FFTW

Asked by Justin on 8 Mar 2013
Latest activity Answered by 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.

  0 Comments

Sign in to comment.

2 Answers

John
John 님의 답변 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.

  0 Comments

Sign in to comment.


Answer by 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.

  0 Comments

Sign in to comment.