If you can give a upper bound of output length, and it’s not large (<= 20 for example), you might try
findmaxs(A::AbstractArray) = begin
inds = Vector{eltype(keys(A))}(undef, 20)
maxval = typemin(eltype(A))
n = 0
@inbounds for i in keys(A)
Ai = A[i]
Ai < maxval && continue
if Ai > maxval
maxval = Ai
n = 1
inds[n] = i
else
inds[n+=1] = i
end
end
resize!(inds, n)
end
For inputs like A = rand(1:1000, 100, 100)
, this save about 18% time cost on my desktop. (I have to say the space for optimization is quite small). If you accept linear index, replacing keys
with eachindex
seems better.