-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.rb
107 lines (78 loc) · 1.96 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
require 'benchmark'
class Elem
attr_accessor :id
def initialize(id)
@id = id
end
end
# Taken from active record
unless Enumerable.method_defined? :each_with_object
Enumerable.module_eval do
def each_with_object(memo)
return to_enum :each_with_object, memo unless block_given?
each do |element|
yield element, memo
end
memo
end
end
end
SAMPLES = 2
[10,1000,5000].each do |size|
ids = size.times.sort_by { rand }
results = ids.size.times.map { |x| Elem.new(x) }
puts "\nArray size = #{size}"
Benchmark.bm(20) do |x|
x.report "each_with_object" do
SAMPLES.times do
result_hash = nil
result_hash = results.each_with_object({}) {|result,result_hash| result_hash[result.id] = result }
ids.map {|id| result_hash[id]}
end
end
x.report "sort" do
SAMPLES.times do
results.sort {|a, b| ids.index(a.id) <=> ids.index(b.id)}
end
end
x.report "sort_cached_indexes" do
SAMPLES.times do
ids_index = {}
ids.each_with_index do |id,index|
ids_index[id] = index
end
results.sort {|a, b| ids_index[a.id] <=> ids_index[b.id]}
end
end
x.report "inject_detect" do
SAMPLES.times do
ids.inject([]){|res, val| res << results.detect {|u| u.id == val}}
end
end
x.report "sort_by" do
SAMPLES.times do
results.sort_by{|obj| ids.index(obj.id)}
end
end
x.report "sort_by_cached_index" do
SAMPLES.times do
ids_index = {}
ids.each_with_index do |id,index|
ids_index[id] = index
end
results.sort_by{|obj| ids_index[obj.id]}
end
end
x.report "sort_by_cached_index2" do
SAMPLES.times do
ids_index = {}
idx = 0
ids.each do |id|
ids_index[id] = idx
++idx
end
results.sort_by{|obj| ids_index[obj.id]}
end
end
end
end