4

I compared the speed of FFT using different methods in python and matlab, the results looked a little weird and I didn't know if I did it right. The code in python are as follows:

from scipy import fft
import pyfftw
import numpy as np
from timeit import Timer

a = pyfftw.empty_aligned((256, 256), dtype='complex128')

# fft using scipy
t = Timer(lambda: fft.fft2(a))
print('Time with scipy.fft: %1.3f seconds' % t.timeit(number=1000))

# fft using pyfftw improved scipy
t = Timer(lambda: pyfftw.interfaces.scipy_fft.fft2(a))
pyfftw.interfaces.cache.enable()
print('Time with pyfftw improved scipy: %1.3f seconds' % t.timeit(number=1000))

# fft using pyfftw
a = pyfftw.empty_aligned((256, 256), dtype='complex128')
b = pyfftw.empty_aligned((256, 256), dtype='complex128')
fft_object = pyfftw.FFTW(a, b)
ar = np.random.randn(256,256)
ai = np.random.randn(256,256)
a[:] = ar + 1j*ai
t = Timer(lambda: fft_object())
print('Time with pyfftw: %1.3f seconds' % t.timeit(number=1000))

with the outputs:

Time with scipy.fft: 1.416 seconds
Time with pyfftw improved scipy: 1.305 seconds
Time with pyfftw: 0.122 seconds

The code in matlab is as follows:

a = zeros(256,256);
a = a + i*a;
tic;
for n = 1:1000
fft2(a);
end
toc;

with the time cost 0.721065 s. The time costs are so different between pyfftw and scipy and between pyfftw and matlab. Did I conduct the comparison correctly and why the differences are so obvious?

BeWu
  • 1,941
  • 1
  • 16
  • 22
shz
  • 159
  • 8
  • 1
    You're only doing a 1D FFT with `fft_object`. Try setting `axes=(0, 1)`. You can also tweak the number of threads as well, which will change the result. I suggest reading the [API docs](https://pyfftw.readthedocs.io/en/latest/source/pyfftw/pyfftw.html#fftw-class). – Henry Gomersall Jun 08 '20 at 08:27
  • Thanks for your kind reply. I set `axes=(0,1)` and now the time cost of `pyfftw.FFTW` is about 0.35 s. I will read the docs for detailed usage of pyfftw. – shz Jun 08 '20 at 11:37

0 Answers0