JuliaMath / FFTW.jl

Julia bindings to the FFTW library for fast Fourier transforms
https://juliamath.github.io/FFTW.jl/stable
MIT License
273 stars 55 forks source link

How to avoid the performance loss of rfft function after using fft function? #305

Closed liushang0322 closed 3 months ago

liushang0322 commented 3 months ago
using FFTW
using Random
a = rand(100000000)
@time rfft(a);
0.307048 seconds (228 allocations: 762.957 MiB, 7.69% gc time)

But when I use fft once first and then use the above code

using FFTW
using Random
a = rand(100000000)
fft(a)
@time rfft(a);
2.986037 seconds (228 allocations: 762.957 MiB, 1.63% gc time)

Performance is about ten times worse,How to avoid the performance loss of rfft function after using fft function?

stevengj commented 3 months ago

I can't replicate your timings on my machine. I suspect that you're just being fooled by the unreliability of @time (in particular, you want to discount the time from the first time you call a function, since not only is some compilation involved, but FFTW also creates a "plan" object that can potentially be re-used. Better to use @btime from the BenchmarkTools.jl package to get reliable timing measurements.

However, if you care about performance, you should be using plan_rfft anyway to create a "plan" (saved algorithm and data tables) for the FFT and then re-use it.