Script that compares the speed of several implementations of the Fast Fourier Transform:
- numpy.fft
- scipy.fftpack
- pyfftw.interfaces.numpy_fft
- pyfftw.interfaces.scipy_fftpack
- pyfftw.builders
- pyfftw.FFTW
- Windows:
virtualenv .venv
cd .venv/Scripts
activate
pip install -r <path_to_project>/requirements.txt
- Linux
virtualenv .venv -p python3
cd .venv/bin
source ./activate
pip install -r <path_to_project>/requirements.txt
For randomly generated one-dimensional and two-dimensional arrays, a direct FFT is performed, then the power spectral density is calculated, and then the inverse FFT is calculated. The procedure is repeated several times, after which the average execution time of the indicated operations is calculated. For one-dimensional arrays, averaging occurs over 50,000 implementations, for two-dimensional arrays, over 500. The resulting times are normalized to the time of the FFT using the library numpy. The results are presented below:
1D array |
2D array |
---|---|
It can be seen that in the case of a one-dimensional array, the pyfftw library modules, simulating the numpy and scipy interfaces, as well as the pyfftw.builders module, lose much in speed. The explanation is that for a one-dimensional array, the time for preliminary internal parameter adjustment, which always happens in pyfftw, is comparable to the time of the entire transfrom. In the case of a two-dimensional array, this time is short, therefore, the implementation of transformations in the indicated modules is faster than in those whose interface they repeat. Note that the FFT from scipy is always a little more efficient than from numpy, and the speed of transform in pyfftw with preliminary creation of plans is at least 2-3 times higher than all the others.