MATLAB: Is it faster to first permute the array such that the last dimension is the dimension you want to FFT and then perform FFT on that dimension

fftMATLABperformancepermutation

Best Answer

The intuition that it would be optimal to permute the dimension you are transforming to be the last dimension is actually the opposite of what you would do if you could gain in performance from permutation. By permuting the dimension you are transforming to the front, then you are performing the FFT down contiguous chunks of memory, which is always going to be better than computing the FFT across strided elements (i.e., computing across anything but the first dimension).

In practice, though, when you permute an array you make a copy of it, and it is likely that the cost of this copy is non-trivial. For example, consider an input:

>> X = randn(1000, 3, 1000);

Transforming down the first dimension of this input is certainly faster than transforming down the third dimension:

>> tic; for i = 1:100, Y = fft(X, 1000, 3); end, toc
Elapsed time is 1.659455 seconds.
>> tic; for i = 1:100, Y = fft(X, 1000, 1); end, toc
Elapsed time is 1.289272 seconds.

However, that does not make permuting it a good idea. For example, consider the following:

>> tic; for i = 1:100, Y = ipermute(fft(permute(X, [3 1 2]), 1000, 1), [3 1 2]); end, toc
Elapsed time is 3.450691 seconds.

The cost of the two permutations needed here is quite large relative to the amount of time spent in FFT.

If you have control over how you store the data from the start, then we recommend you to try storing it such that you can operate on the first dimension. This is in general a good idea for any operation -- whenever you can operate along the first dimension of a matrix/N-D array, you will be accessing contiguous memory and things will likely go faster. However, permuting just to perform the FFT down the first dimension will most likely not be a good idea -- it either will not make a tangible difference or it will make the code slower as a whole.