forked from sdevani/sorted_sets
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sorted_set_speed_test.rb
116 lines (97 loc) · 2.14 KB
/
sorted_set_speed_test.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
REPS = 100
COUNT = 10000
class SortedSetTest
def self.run_tests(sets)
@@results = {}
sets.each do |set|
@@results[set.name] = test_set(set)
end
end
def self.test_set(set)
SpeedTest.test(SetTester.new(set))
end
def self.print_all
print_results(:sort)
print_results(:include)
print_results(:insert)
end
def self.print_results(method)
puts "#{method} results"
puts "=============="
print_col "Set Size"
@@results.keys.each do |set_key|
print_col @@results[set_key][:name]
end
print "\n"
10.times do |i|
print_col (i+1)*COUNT/10
@@results.keys.each do |set_key|
print_col @@results[set_key][method][i]
end
print "\n"
end
end
def self.print_col(input)
print "#{input.to_s.ljust(10).slice(0,10)}\t"
end
end
class SetTester
def initialize(set)
@set = set
end
def name
@set.name
end
def init(size, reps)
@reps = reps
size.times do |x|
@set.insert "test #{x}abcdefghijklmnopzz".split('').shuffle.join('')
end
end
def call_sort
@reps.times do
@set.get_sorted_array
end
end
def call_insert
@reps.times do |x|
@set.insert "abcdefghijklmnopqrstuvwxyz#{x}".split('').shuffle.join('')
end
end
def call_include
@reps.times do
@set.include? "abc"
end
end
end
class SpeedTest
@@reps = REPS
def self.test(subject, count=COUNT)
results = {
include: [],
sort: [],
insert: []
}
results[:name] = subject.name
10.times do |fraction|
size = count * (fraction + 1) / 10
subject.init(size, @@reps)
start = Time.now
subject.call_include
finish = Time.now
results[:include] << (finish - start)
start = Time.now
subject.call_sort
finish = Time.now
results[:sort] << (finish - start)
start = Time.now
subject.call_insert
finish = Time.now
results[:insert] << (finish - start)
end
return results
end
end
require_relative 'sorted_set'
SortedSetTest.run_tests([BasicArraySortedSet.new, ArraySortedSet.new, HashSortedSet.new])
SortedSetTest.print_all