julia> using DataStructures
julia> function MaxN7(cr,N)
maxn = heapify!(cr[1:N])
maxn1=maxn[1]
@inbounds for i in N+1:length(cr)
e=cr[i]
if maxn1 < e
heappop!(maxn)
heappush!(maxn,e)
maxn1=maxn[1]
end
end
sort!(maxn,rev=true)
end
MaxN7 (generic function with 1 method)
# on my laptop
julia> @btime MaxN7(x, 5) setup=(x=randn(1000));
722.951 ns (1 allocation: 96 bytes)
julia> @btime bottomk(x, 5) setup=(x=randn(1000));
752.137 ns (5 allocations: 320 bytes)
3 Likes