This seems to be significantly faster than Base’s tuple sorting in both runtime and compile time.
julia> for i in 1:20
println(i)
x = Tuple(rand(i))
@time sort(x)
@time sorted(x)
@btime sort($x)
@btime sorted($x)
end
1
0.001746 seconds (2.28 k allocations: 108.430 KiB, 98.43% compilation time)
0.001823 seconds (3.61 k allocations: 169.359 KiB, 99.16% compilation time)
1.375 ns (0 allocations: 0 bytes)
1.375 ns (0 allocations: 0 bytes)
2
0.003862 seconds (14.96 k allocations: 756.289 KiB, 99.35% compilation time)
0.002913 seconds (5.99 k allocations: 292.672 KiB, 99.49% compilation time)
1.666 ns (0 allocations: 0 bytes)
1.375 ns (0 allocations: 0 bytes)
3
0.005494 seconds (20.86 k allocations: 1.080 MiB, 99.44% compilation time)
0.002853 seconds (11.49 k allocations: 585.125 KiB, 99.53% compilation time)
3.083 ns (0 allocations: 0 bytes)
1.666 ns (0 allocations: 0 bytes)
4
0.010172 seconds (42.30 k allocations: 2.316 MiB, 99.74% compilation time)
0.003560 seconds (14.91 k allocations: 772.594 KiB, 99.47% compilation time)
4.208 ns (0 allocations: 0 bytes)
2.166 ns (0 allocations: 0 bytes)
5
0.013185 seconds (38.95 k allocations: 2.096 MiB, 99.78% compilation time)
0.003609 seconds (21.28 k allocations: 1.079 MiB, 99.51% compilation time)
7.083 ns (0 allocations: 0 bytes)
3.916 ns (0 allocations: 0 bytes)
6
0.026137 seconds (65.27 k allocations: 3.628 MiB, 99.88% compilation time)
0.004008 seconds (26.30 k allocations: 1.329 MiB, 99.60% compilation time)
8.625 ns (0 allocations: 0 bytes)
5.250 ns (0 allocations: 0 bytes)
7
0.025794 seconds (51.51 k allocations: 2.736 MiB, 99.88% compilation time)
0.005348 seconds (32.75 k allocations: 1.734 MiB, 99.71% compilation time)
9.926 ns (0 allocations: 0 bytes)
6.958 ns (0 allocations: 0 bytes)
8
0.038604 seconds (67.09 k allocations: 3.516 MiB, 99.93% compilation time)
0.005249 seconds (37.66 k allocations: 1.988 MiB, 99.66% compilation time)
16.241 ns (0 allocations: 0 bytes)
8.334 ns (0 allocations: 0 bytes)
9
0.022513 seconds (55.22 k allocations: 3.057 MiB, 99.88% compilation time)
0.006660 seconds (47.14 k allocations: 2.506 MiB, 99.82% compilation time)
19.664 ns (0 allocations: 0 bytes)
11.094 ns (0 allocations: 0 bytes)
10
0.329988 seconds (1.23 M allocations: 67.375 MiB, 99.99% compilation time)
0.009091 seconds (56.44 k allocations: 2.971 MiB, 99.72% compilation time)
50.489 ns (2 allocations: 144 bytes)
13.598 ns (0 allocations: 0 bytes)
11
0.014698 seconds (79.00 k allocations: 4.314 MiB, 99.69% compilation time)
0.009332 seconds (62.86 k allocations: 3.298 MiB, 99.85% compilation time)
60.568 ns (2 allocations: 144 bytes)
15.364 ns (0 allocations: 0 bytes)
12
0.018018 seconds (83.03 k allocations: 4.528 MiB, 99.71% compilation time)
ERROR: MethodError: no method matching sorted(::OptimalSortingNetworks.var"#2#3"{typeof(identity), typeof(<)}, ::NTuple{12, Float64}, ::Depth)
Closest candidates are:
sorted(::Any, ::NTuple{10, Any}, ::Union{Depth, Size})
@ OptimalSortingNetworks ~/.julia/packages/OptimalSortingNetworks/MzN3U/src/OptimalSortingNetworks.jl:111
sorted(::Any, ::Union{Tuple{}, Tuple{Any}, Tuple{Any, Any}, Tuple{Any, Any, Any}, NTuple{4, Any}, NTuple{5, Any}, NTuple{6, Any}, NTuple{7, Any}, NTuple{8, Any}, NTuple{9, Any}, NTuple{11, Any}}, ::Union{Depth, Size})
@ OptimalSortingNetworks ~/.julia/packages/OptimalSortingNetworks/MzN3U/src/OptimalSortingNetworks.jl:106
sorted(::Any, ::NTuple{10, Any})
@ OptimalSortingNetworks ~/.julia/packages/OptimalSortingNetworks/MzN3U/src/OptimalSortingNetworks.jl:111
...
Stacktrace:
[1] sorted(t::NTuple{12, Float64}, o::Depth)
@ OptimalSortingNetworks ~/.julia/packages/OptimalSortingNetworks/MzN3U/src/OptimalSortingNetworks.jl:99
[2] macro expansion
@ ./timing.jl:282 [inlined]
[3] top-level scope
@ ./REPL[7]:5
PRs to base welcome