Any sorting function in Julia that is as fast as Matlab's maxk?

How large is your array? My SortingLab.jl has a fast sortperm (fsortperm) for sorting integers but not floats. It’s not hard to adapt the algorithm for floats as follows. The issue with fsortperm is that it doesn’t know to stop once it has found the top k values but instead returns everything first so it may not be as efficient.

# uint_mapping stolen from SortingAlgorithms.jl
uint_mapping(x::Float64)  = (y = reinterpret(Int64, x); reinterpret(UInt64, ifelse(y < 0, ~y, xor(y, typemin(Int64)))))

maxk(Q,k) = begin
    Qunit = uint_mapping.(Q)
    res = SortingLab.fsortperm(Qunit)
    Q[res[end-(k-1):end]]
end

Q = randn(10000)
@benchmark maxk($Q, 10)

those are the results from Juliabox (v0.6.2), so I think it’s pretty good for such a small size Q (length = 1000)

BenchmarkTools.Trial: 
  memory estimate:  133.78 KiB
  allocs estimate:  34
  --------------
  minimum time:     97.704 μs (0.00% GC)
  median time:      115.204 μs (0.00% GC)
  mean time:        139.580 μs (5.07% GC)
  maximum time:     100.319 ms (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

Also the below is a better way to benchmark

using BenchmarkTools
@benchmark ind1=partialsortperm($Q, 1:10, rev=true);

would be a better way