forked from greensync/interval-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rb
178 lines (133 loc) · 5.16 KB
/
benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'benchmark'
require_relative "lib/two_lists_iterative_median"
require_relative "lib/two_lists_iterative_average"
require_relative "lib/two_lists_iterative_average_max_end"
require_relative "lib/one_list_recursive_average"
require_relative "lib/one_list_recursive_median"
require_relative "lib/one_list_iterative_average"
require_relative "lib/one_list_iterative_median"
require_relative "lib/one_list_iterative_average_no_rational"
require_relative "lib/one_list_iterative_median_no_rational"
require_relative "lib/one_list_iterative_average_max_end"
include TwoListsIterativeMedian
include TwoListsIterativeAverage
include OneListRecursiveAverage
include OneListRecursiveMedian
include OneListIterativeAverage
include OneListIterativeMedian
include OneListIterativeAverageNoRational
include OneListIterativeMedianNoRational
include OneListIterativeAverageMaxEnd
def generate_random_intervals(min_start, max_end, interval_count)
intervals = []
(0...interval_count).each do |itv|
a = rand(min_start..max_end)
range = [a, a+1000]
intervals << [range.min...range.max]
end
intervals
end
def generate_random_point_queries(min_start, max_end, query_count)
queries = []
(0...query_count).each do |q|
queries << rand(min_start..max_end)
end
queries
end
def pretty_print(res)
#puts "User CPU time: #{res.utime}"
#puts "System CPU time: #{res.stime}"
#puts "User + System CPU time: #{res.total}"
puts "Total elapsed time: #{res.real.round(4)}"
puts ""
end
def main(a, b, interval_count, query_count)
puts ""
puts("Generating #{interval_count} intervals starting in range [#{a}...#{b}]")
itvs = generate_random_intervals(a, b, interval_count)
puts("Generating #{query_count} queries")
puts ""
queries = generate_random_point_queries(a, b, query_count)
tree = OneListRecursiveAverage::Tree.new(itvs)
puts "Recursive implementation with one list per node, using average (s_center not sorted)"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = OneListRecursiveMedian::Tree.new(itvs)
puts "Recursive implementation with one list per node, using median (s_center not sorted)"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = OneListIterativeAverage::Tree.new(itvs)
puts "Iterative implementation with one list per node using average (s_center sorted), not taking maximum interval end into account [Previous version]"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = OneListIterativeAverageMaxEnd::Tree.new(itvs)
puts "Iterative implementation with one list per node using average (s_center sorted), taking maximum interval end into account [Pushed version]"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = OneListIterativeMedian::Tree.new(itvs)
puts "Iterative implementation with one list per node using median (s_center sorted), taking maximum interval end into account"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = TwoListsIterativeAverage::Tree.new(itvs)
puts "Iterative implementation with two lists per node, using average (s_center sorted)"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = TwoListsIterativeMedian::Tree.new(itvs)
puts "Iterative implementation with two lists per node, using median (s_center sorted)"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = TwoListsIterativeAverageMaxEnd::Tree.new(itvs)
puts "Iterative implementation with two lists per node, using average (s_center sorted), taking maximum interval end into account"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = OneListIterativeAverageNoRational::Tree.new(itvs)
puts "Using average (s_center sorted), taking maximum interval end into account, no rationals"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
tree = OneListIterativeMedianNoRational::Tree.new(itvs)
puts "Using median (s_center sorted), taking maximum interval end into account, no rationals"
res = Benchmark.measure {
queries.each do |stab|
tree.search(stab, unique: false, sort: false)
end
}
pretty_print(res)
puts "See tests: rspec spec/interval_tree_spec.rb"
end
main(0, 100_000, 10_000, 100_000)