3

我在python和matlab中比较了不同方法的FFT速度,结果看起来有点怪,不知道是不是做对了。python中的代码如下:

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))

输出:

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

matlab中的代码如下:

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

时间成本为 0.721065 秒。pyfftw 和 scipy 以及 pyfftw 和 matlab 之间的时间成本是如此不同。我是否正确进行了比较,为什么差异如此明显?

4

0 回答 0