Skip to content

Commit 4b39ce4

Browse files
authored
Merge pull request #305 from LilithHafner/lh/sort
rewrite SortBenchmarks.jl
2 parents a58af11 + 69cd3d5 commit 4b39ce4

File tree

1 file changed

+106
-27
lines changed

1 file changed

+106
-27
lines changed

src/sort/SortBenchmarks.jl

Lines changed: 106 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,131 @@ module SortBenchmarks
33
include(joinpath(dirname(@__FILE__), "..", "utils", "RandUtils.jl"))
44

55
using .RandUtils
6+
using Random
67
using BenchmarkTools
78

89
const SUITE = BenchmarkGroup()
9-
const LIST_SIZE = 50000
10-
const LISTS = (
11-
("ascending", collect(1:LIST_SIZE)),
12-
("descending", collect(LIST_SIZE:-1:1)),
13-
("ones", ones(LIST_SIZE)),
14-
("random", samerand(LIST_SIZE))
15-
)
1610

17-
#####################################
18-
# QuickSort/MergeSort/InsertionSort #
19-
#####################################
11+
################################################
12+
# Default algorithms at various sizes (≈162s) #
13+
################################################
2014

21-
for (group, Alg) in (("quicksort", QuickSort), ("mergesort", MergeSort), ("insertionsort", InsertionSort))
22-
g = addgroup!(SUITE, group)
23-
for (kind, list) in LISTS
24-
ix = collect(1:length(list))
25-
g["sort forwards", kind] = @benchmarkable sort($list; alg = $Alg)
26-
g["sort reverse", kind] = @benchmarkable sort($list; alg = $Alg, rev = true)
27-
g["sortperm forwards", kind] = @benchmarkable sortperm($list; alg = $Alg)
28-
g["sortperm reverse", kind] = @benchmarkable sortperm($list; alg = $Alg, rev = true)
29-
g["sort! forwards", kind] = @benchmarkable sort!(x; alg = $Alg) setup=(x = copy($list))
30-
g["sort! reverse", kind] = @benchmarkable sort!(x; alg = $Alg, rev = true) setup=(x = copy($list))
31-
g["sortperm! forwards", kind] = @benchmarkable sortperm!(x, $list; alg = $Alg) setup=(x = copy($ix))
32-
g["sortperm! reverse", kind] = @benchmarkable sortperm!(x, $list; alg = $Alg, rev = true) setup=(x = copy($ix))
15+
for LENGTH in [3, 10, 30, 100, 1000, 10000]
16+
g = addgroup!(SUITE, "length = $LENGTH")
17+
18+
g["sort!(fill(missing, length), rev=true)"] = @benchmarkable sort!(x, rev=true) setup=(x=fill(missing, $LENGTH))
19+
g["sort!(fill(missing, length))"] = @benchmarkable sort!(x) setup=(x=fill(missing, $LENGTH))
20+
g["sort!(rand(Int, length))"] = @benchmarkable sort!(rand!(x)) setup=(x=rand(Int, $LENGTH)) # 47474
21+
g["sort(randn(length))"] = @benchmarkable sort(x) setup=(x=randn($LENGTH))
22+
23+
g["sortperm(rand(length))"] = @benchmarkable sortperm(x) setup=(x=rand($LENGTH))
24+
25+
g["mixed eltype with by order"] = @benchmarkable sort(x; by=x -> x isa Symbol ? (0, x) : (1, x)) setup=(x=[rand() < .5 ? randstring() : Symbol(randstring()) for _ in 1:$LENGTH])
26+
27+
# 27781
28+
g["Float64 unions with missing"] = @benchmarkable sort(x) setup=(x=shuffle!(vcat(fill(missing, $(LENGTH÷10)), rand($(LENGTH*9÷10)))))
29+
g["Int unions with missing"] = @benchmarkable sort(x) setup=(x=shuffle!(vcat(fill(missing, $(LENGTH÷10)), rand(Int, $(LENGTH*9÷10)))))
30+
31+
n = ceil(Int, cbrt(LENGTH/4))
32+
for i in 1:3 # 47538, 45326
33+
g["sort(rand(2n, 2n, n); dims=$i)"] = @benchmarkable sort(rand(2*$n,2*$n,$n); dims=$i)
34+
g["sort!(rand(2n, 2n, n); dims=$i)"] = @benchmarkable sort!(rand(2*$n,2*$n,$n); dims=$i)
3335
end
36+
37+
g["ascending"] = @benchmarkable sort!(x) setup=(x=sort(rand($LENGTH)))
38+
g["descending"] = @benchmarkable sort(x) setup=(x=sort(rand($LENGTH)))
39+
g["all same"] = @benchmarkable sort!(x) setup=(x=fill(rand(), $LENGTH))
40+
3441
for b in values(g)
35-
b.params.time_tolerance = 0.30
42+
b.params.time_tolerance = 0.20
3643
end
3744
end
3845

39-
############
40-
# issorted #
41-
############
46+
##################
47+
# Issues (≈56s) #
48+
##################
49+
50+
let g = addgroup!(SUITE, "issues")
51+
52+
# 939
53+
g["sortperm(collect(1000000:-1:1))"] = @benchmarkable sortperm(x) setup=(x=collect(1000000:-1:1))
54+
g["sortperm(rand(10^5))"] = @benchmarkable sortperm(x) setup=(x=rand(10^5))
55+
g["sortperm(rand(10^7))"] = @benchmarkable sortperm(x) setup=(x=rand(10^7))
56+
57+
# 9832
58+
a_9832 = rand(Int, 30_000_000, 2)
59+
g["sortslices sorting very short slices"] = @benchmarkable sortslices($a_9832, dims=2)
60+
61+
# 36546
62+
xv_36546 = view(rand(1000), 1:1000)
63+
g["sortperm on a view (Float64)"] = @benchmarkable sortperm($xv_36546)
64+
xs_36546 = rand(1:10^3, 10^4, 2)
65+
g["sortperm on a view (Int)"] = @benchmarkable sortperm(view($xs_36546,:,1))
66+
67+
# 39864
68+
v2_39864 = samerand(2000)
69+
g["inplace sorting of a view"] = @benchmarkable sort!(vv) setup = (vv1 = deepcopy($v2_39864); vv = @view vv1[500:1499]) evals = 1
70+
71+
# 46149
72+
g["Float16"] = @benchmarkable sort!(x) setup=(x=rand(Float16, 10^6)) evals=1
73+
74+
# 47152
75+
g["small Int view"] = @benchmarkable sort!(view(x,2:4)) setup=(x = [2,1,10,15,20])
76+
g["small Float64 view"] = @benchmarkable sort!(view(x,2:4)) setup=(x = [2.0,1.0,10.0,15.0,20.0])
77+
78+
# 47191
79+
g["partialsort(rand(10_000), 10_000)"] = @benchmarkable partialsort(x, 10_000) setup=(x=rand(10_000))
80+
81+
# 47715
82+
g["sort(rand(10^8))"] = @benchmarkable sort(x) setup=(x=rand(10^8))
83+
84+
# 47766
85+
g["partialsort!(rand(10_000), 1:3, rev=true)"] = @benchmarkable partialsort!(x, 1:3; rev=true) setup=(x=rand(10_000)) evals=1
86+
for b in values(g)
87+
b.params.time_tolerance = 0.20
88+
end
89+
end
90+
91+
#############################################
92+
# QuickSort/MergeSort/InsertionSort (≈47s) #
93+
#############################################
94+
95+
for (group, Alg, len) in (("quicksort", QuickSort, 50_000), ("mergesort", MergeSort, 50_000), ("insertionsort", InsertionSort, 100))
96+
list = samerand(len)
97+
g = addgroup!(SUITE, group)
98+
99+
ix = collect(1:length(list))
100+
g["sort forwards"] = @benchmarkable sort($list; alg = $Alg)
101+
g["sortperm forwards"] = @benchmarkable sortperm($list; alg = $Alg)
102+
g["sort! reverse"] = @benchmarkable sort!(x; alg = $Alg, rev = true) setup=(x = copy($list)) evals=1
103+
g["sortperm! reverse"] = @benchmarkable sortperm!(x, $list; alg = $Alg, rev = true) setup=(x = copy($ix))
104+
105+
for b in values(g)
106+
b.params.time_tolerance = 0.20
107+
end
108+
end
109+
110+
####################
111+
# issorted (≈10s) #
112+
####################
42113

43114
g = addgroup!(SUITE, "issorted")
44115

116+
const LIST_SIZE = 50_000
117+
const LISTS = (
118+
("ascending", collect(1:LIST_SIZE)),
119+
("descending", collect(LIST_SIZE:-1:1)),
120+
("ones", ones(LIST_SIZE)),
121+
("random", samerand(LIST_SIZE))
122+
)
123+
45124
for (kind, list) in LISTS
46125
g["forwards", kind] = @benchmarkable issorted($list)
47126
g["reverse", kind] = @benchmarkable issorted($list; rev = true)
48127
end
49128

50129
for b in values(g)
51-
b.params.time_tolerance = 0.30
130+
b.params.time_tolerance = 0.20
52131
end
53132

54133
end # module

0 commit comments

Comments
 (0)