I have not found among the library functions a function that finds the first N maximum values of a list. I tried to define some functions and make performance comparisons. I found two kinds of problems: one was to find an effective algorithm; a second how to measure the performance between different algorithms. I would like to know if there is a similar function in some package or if some algorithm is known that works “better” than others. I would like to have information on how, with the same algorithms proposed, I can improve performance. Since one of the algorithms “consumes” the list to which it applies and this can change the result of @btime I had to insert an item that copies the input vector in the maxN3 function, which would not be strictly necessary for the function’s task .
How could I compare performance on equal terms, or alternatively, how could I change the function to have the same algorithm but that doesn’t “consume” the list. Then, for example, replace pop! with other functions, but without penalizing too much performance.
r=rand(1:10^9, 10^7) maxN1(s,N,lst=) = N==0 ? lst : maxN1(setdiff(s,maximum(s)),N-1,push!(lst,maximum(s))) maxN2(s,N)= sort(s,rev=true)[1:N] function MaxN3(cr,N) maxn=Int r=copy(cr) for x in 1:N p=pop!(r) for (i,e) in enumerate(r) if p < e r[i],p=p,r[i] end end push!(maxn,p) end maxn end