-
Notifications
You must be signed in to change notification settings - Fork 152
implement sortperm #773
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
implement sortperm #773
Conversation
Great questions, and very neat PR, thanks! For a combination of both stability and speed, sorting a vector of Tuple like I did in my first attempt at #772 seemed quite fast. So perhaps there's a way to adapt that approach to the fully featured version? For |
c6d97cf
to
aa340e2
Compare
Thank you for the comments! @inline sortperm2(v::StaticVector) = last.(sort(tuple.(v,SVector{length(v)}(1:length(v)))))
v = rand(SVector{4,Int})
@btime sortperm($(Ref(v))[]) # 9.158 ns (0 allocations: 0 bytes)
@btime sortperm2($(Ref(v))[]) # 5.737 ns (0 allocations: 0 bytes)
v = rand(SVector{19,Int})
@btime sortperm($(Ref(v))[]) # 105.775 ns (0 allocations: 0 bytes)
@btime sortperm2($(Ref(v))[]) # 284.212 ns (0 allocations: 0 bytes) Can you confirm? I'm not sure about what approach is better. |
I'm not sure what the answer is. Keep in mind that the output of |
I agree, as @c42f originally mentioned |
I really don’t mind. SVector seems fine to me. I’d be happy to merge like that and iterate on the “similar” approach in the future. |
So, with the tuple-based implementation (rather than the default julia> mutable struct S
i::Int
id::Symbol
end
julia> Base.isless(a::S, b::S) = isless(a.i, b.i)
julia> sort([S(1,:a), S(1,:b)], rev=true)
2-element Array{S,1}:
S(1, :a)
S(1, :b)
julia> sort(SA[S(1,:a), S(1,:b)], rev=true)
2-element SArray{Tuple{2},S,1,2} with indices SOneTo(2):
S(1, :b)
S(1, :a) In base an explicit Is there some slightly different way to formulate this so that it's both fast and consistent with Base? |
This is an interesting question. While I think lexicographic ordering with respect to |
By the way @stev47, the following seems interesting and possibly relevant https://blog.reverberate.org/2020/05/29/hoares-rebuttal-bubble-sorts-comeback.html |
Good catch! The additional comparison on the index in Base was actually introduced as a workaround for the floating point comparison quirks (see JuliaLang/julia@d427efd). I couldn't come up with a faster version than what is currently in Base though. The natural way would be to just have Thank you for the link. Sorting networks are actually already branchless by design, vectorization on the other hand would definitely be nice-to-have. |
Well I think as a general purpose library we need to be correct first, and fast second. Having a closer look, it seems to me that we can't use the tuple trick in any case because The Tuple comparison based version may be faster because it assumes a logical relationship between |
Beautiful, thanks for continuing to push this through. |
fixes #772
similar_type
for the index array a good idea?