/
array_include.rb
47 lines (37 loc) · 1.05 KB
/
array_include.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
require 'benchmark/ips'
require 'set'
array = %w{
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.ms-powerpoint
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/pdf
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
text/plain
application/rtf
image/jpeg
}
array_symbols = Array.new(array.map(&:to_sym))
set = Set.new(array)
set_symbols = Set.new(array_symbols)
missing_element = 'no such element'
missing_element_sym = missing_element.to_sym
puts
puts 'miss'
puts
Benchmark.ips do |x|
x.report('array') { array.include?(missing_element) }
x.report('array symbols') { array_symbols.include?(missing_element_sym) }
x.compare!
end
existing_element = 'application/pdf'
existing_element_sym = existing_element.to_sym
puts
puts 'hit'
puts
Benchmark.ips do |x|
x.report('array') { array.include?(existing_element) }
x.report('array symbols') { array_symbols.include?(existing_element_sym) }
x.compare!
end