DSP.jl icon indicating copy to clipboard operation
DSP.jl copied to clipboard

conv is slower than necessary

Open KristofferC opened this issue 8 years ago • 2 comments

Moved from https://github.com/JuliaLang/julia/issues/13996 (more discussion there)

It seems that conv is slower than necessary. By implementing a naive convolution, I am able to outperform conv for moderate vector sizes. On my particular machine, naive convolution is better than conv if the product of vector sizes is less than 100 000.

Just so everyone sees what the naive solution is, I'm pasting it below.

slowconvcost(m,n) = 2e-9*m*n+1e-6
fastconvcost(m,n) = 7e-8*(m+n)*log(m+n)+1e-4
function sebconv(a::Array{T}, b::Array{T}) where T
    m = length(a)
    n = length(b)
    if(fastconvcost(m,n)<slowconvcost(m,n)) return conv(a,b); end
    c = zeros(T,m+n-1)
    @inbounds for j=1:m
        for k=1:n
            c[j+k-1] += a[j]*b[k]
        end
    end
    return c
end

Here is a performance plot:

convperf

Edit: tweaked code for better heuristic as to when slowconv is faster than fastconv.

KristofferC avatar Jun 25 '17 05:06 KristofferC

There's still a large gap for small sizes:

julia> x = rand(100); y = rand(100);

julia> @btime sebconv($x, $y);
  1.429 μs (1 allocation: 1.77 KiB)

julia> @btime conv($x, $y);
  17.875 μs (18 allocations: 9.84 KiB)

ViralBShah avatar Feb 24 '24 04:02 ViralBShah

Ref. #545.

martinholters avatar Feb 24 '24 13:02 martinholters