-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rb
executable file
·154 lines (94 loc) · 3.18 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
require 'benchmark'
require 'pry'
F = Struct.new(:name, :size) do
def grouping
"#{name}/#{size}"
end
end
N = 1
files = []
1000.times { files << F.new("#{%w(a b c d e).sample}.txt", (1..5).to_a.sample) }
def test_duplicates_original(files)
result = {}
names = []
files.each do |f|
names << [f.name, f.size]
end
duplicates = names.select{|item| names.count(item) > 1}.uniq
dup_hash_keys = duplicates.flatten
name_keys = dup_hash_keys.values_at(*dup_hash_keys.each_index.select {|i| i.even?})
dup_files = []
files.each do |file|
dup_files << file if duplicates.include?([file.name, file.size])
end
name_keys.each do |key|
result[key] = []
dup_files.each do |file|
result[key] << file if file.name == key && !file.nil? && !key.nil?
end
end
result
end
def test_duplicates_optimized(files)
result = {}
names = files.collect{|f| [f.name, f.size]}
duplicates = names.select{|item| names.count(item) > 1}.uniq
dup_hash_keys = duplicates.flatten
name_keys = dup_hash_keys.values_at(*dup_hash_keys.each_index.select {|i| i.even?})
dup_files = files.select{|i| duplicates.include?([i.name, i.size])}
name_keys.each do |key|
result[key] = dup_files.select{|f| f.name == key }
end
result
end
def test_inject(files)
files.group_by{|e|"#{e.name}/#{e.size}"}.inject({}){|res, (k, v)| res[k.split('/')[0]] ||=[]; res[k.split('/')[0]] << v if v.size > 1; res}
end
def test_duplicates2(files)
result = {}
files.each do |file|
result[file.name] ||= []
result[file.name] << files.select{|f| f.size == file.size && file.name == f.name}
end
result.each do |key, val|
result[key] = val.select{|i| val.count(i) > 1}.uniq.flatten
end
result = result.reject{|k,v| v.empty? }
result
end
a = test_duplicates_original(files)
b = test_duplicates_optimized(files)
c = test_duplicates2(files)
d = test_inject(files)
# puts files
puts c
puts "____________________________qq"
puts d
#raise "c != d" unless c == d
Benchmark.bm do |x|
x.report("my_method original") { N.times { test_duplicates_original(files) } }
x.report("my_method optimized") { N.times { test_duplicates_optimized(files) } }
x.report("test_duplicates2") { N.times { test_duplicates2(files) } }
x.report("inject") { N.times { test_inject(files) } }
end
# def self.duplicates(files)
# result = {}
# names = []
# files.each do |f|
# names << [f.item_file_name, f.item_file_size]
# end
# duplicates = names.select{|item| names.count(item) > 1}.uniq
# dup_hash_keys = duplicates.flatten
# name_keys = dup_hash_keys.values_at(* dup_hash_keys.each_index.select {|i| i.even?})
# dup_files = []
# files.each do |file|
# dup_files << file if duplicates.include?([file.item_file_name, file.item_file_size])
# end
# name_keys.each do |key|
# result[key] = []
# dup_files.each do |file|
# result[key] << file if file.item_file_name == key && !file.nil? && !key.nil?
# end
# end
# result
# end