Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance #15

Open
henry2004y opened this issue Apr 30, 2021 · 1 comment · May be fixed by #42
Open

Performance #15

henry2004y opened this issue Apr 30, 2021 · 1 comment · May be fixed by #42

Comments

@henry2004y
Copy link

I am a little bit surprised about the performance of argmaxima compared with a direct implementation:

using Test, BenchmarkTools
using Peaks

function localmaxmin!(y, maxs::Vector{Int}, mins::Vector{Int})
   empty!(maxs)
   empty!(mins)
   for i in 2:length(y)-1
       if y[i+1] < y[i] > y[i-1]
           push!(maxs, i)
       elseif y[i+1] > y[i] < y[i-1]
           push!(mins, i)
       end
   end
end

function localmaxmin2!(x, maxs, mins)
   append!(maxs2, argmaxima(x))
   append!(mins2, argminima(x))
   maxs2, mins2
end

function localmax(y)
   maxs = Int[]
   for i in 2:length(y)-1
       if y[i+1] < y[i] > y[i-1]
           push!(maxs, i)
       end
   end
   maxs
end

function localmax2(x)
   maxs2 = argmaxima(x)
end

maxs = Int[]
mins  = Int[]
maxs2= Int[]
mins2 = Int[]
x = zeros(100000)
x_max = 2:100:100000
x[x_max] .= 1
x_min = 5:100:100000
x[x_min] .= -1
localmaxmin!(x, maxs, mins)
localmaxmin2!(x, maxs2, mins2)

@test maxs == maxs2
@test mins == mins2

@btime localmax(x)
@btime localmax2(x)
@test localmax(x) == localmax2(x)

On my laptop, this returns

102.428 μs (10 allocations: 16.39 KiB)
380.387 μs (6 allocations: 398.92 KiB)
Test Passed

which is 3 times slower.

@halleysfifthinc
Copy link
Owner

Actually its almost 4x slower! I too am surprised by the size of the difference in performance! I'm also surprised about the difference in allocation size.

The argmaxima function has more features which explains at least some of the decreased performance compared to your localmax function: argmaxima supports larger/custom window sizes, identification of plateaus, and handles the presence of NaNs and missings. The latter two features likely contribute the bulk of the increased runtime.

In any case, if the localmax function meets your needs, it doesn't appear to that switching to Peaks.jl would be beneficial, at the moment.

Repository owner deleted a comment from a-11-hacker May 15, 2021
@halleysfifthinc halleysfifthinc mentioned this issue Jan 23, 2024
14 tasks
@halleysfifthinc halleysfifthinc linked a pull request Apr 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants