Skip to content

Commit a4f5182

Browse files
committed
Add some basic tree benchmarks
1 parent bf66dd1 commit a4f5182

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

benchmark/bench_trees.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module BenchTrees
2+
3+
using DataStructures
4+
using BenchmarkTools
5+
using Random
6+
7+
suite = BenchmarkGroup()
8+
9+
rand_setup = (
10+
Random.seed!(1234);
11+
idx1 = rand(1:30000, 1000);
12+
didx1 = rand(1:1000, 500);
13+
)
14+
15+
# insert a bunch of keys, search for all of them, delete half of them randomly,
16+
# then search for all of them again.
17+
function test_rand(T)
18+
t = T{Int}()
19+
for i in idx1
20+
push!(t, i)
21+
end
22+
for i in idx1
23+
haskey(t, i)
24+
end
25+
for i in didx1
26+
delete!(t, idx1[i])
27+
end
28+
for i in idx1
29+
haskey(t, i)
30+
end
31+
end
32+
33+
# insert 1, ..., N, then push 1 many times. tests a regression from an older
34+
# splay tree implementation where splays didn't happen on redundant pushes.
35+
function test_redundant(T, N=100000)
36+
t = T{Int}()
37+
for i in 1:N
38+
push!(t, i)
39+
end
40+
for i in 1:N
41+
push!(t, 1)
42+
end
43+
end
44+
45+
# insert 1, ..., N, then access element 1 for N iterations. splay trees should
46+
# perform best here.
47+
function test_biased(T, N=100000)
48+
t = T{Int}()
49+
for i in 1:N
50+
push!(t, i)
51+
end
52+
for _ in 1:N
53+
haskey(t, 1)
54+
end
55+
end
56+
57+
trees = Dict("splay" => SplayTree, "avl" => AVLTree, "rb" => RBTree)
58+
for (T_name, T) in trees
59+
suite[T_name]["rand"] =
60+
@benchmarkable test_rand($(T)) setup=rand_setup
61+
suite[T_name]["redundant"] =
62+
@benchmarkable test_redundant($(T))
63+
suite[T_name]["biased"] =
64+
@benchmarkable test_biased($T)
65+
end
66+
67+
end # module
68+
69+
BenchTrees.suite

benchmark/benchmarks.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ const SUITE = BenchmarkGroup()
1212

1313
SUITE["heap"] = include("bench_heap.jl")
1414
SUITE["SparseIntSet"] = include("bench_sparse_int_set.jl")
15+
SUITE["trees"] = include("bench_trees.jl")

src/DataStructures.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ module DataStructures
5353
export RBTree, search_node, minimum_node
5454
export SplayTree, search_node!, maximum_node
5555
export AVLTree, sorted_rank
56-
export SplayTree, maximum_node
5756

5857
export findkey
5958

0 commit comments

Comments
 (0)