-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_wrapping.rb
65 lines (59 loc) · 1.57 KB
/
array_wrapping.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
require 'ostruct'
require 'benchmark'
# Make sure you `gem install benchmark-ips`
require 'benchmark/ips'
arrays = 100.times.collect {|i| (rand(10) + 1).times.collect { rand(100) } }
non_arrays = 100.times.collect do |i|
case rand(3)
when 0
i + 1
when 1
"#{i + 1}"
when 2
i % 2 == 0
end
end.shuffle
mixed = (arrays[0...50] + non_arrays[0...50]).shuffle
benchmark_lambda = lambda do |x|
# Worst case - none need operation
x.report("`Array()` arrays") do
arrays.map {|i| Array(i) }
end
x.report("`[].flatten` arrays") do
arrays.map {|i| [i].flatten }
end
x.report("`Array()` arrays with check") do
arrays.map {|i| i.is_a?(Array) ? i : Array(i) }
end
x.report("`[]` arrays with check") do
arrays.map {|i| i.is_a?(Array) ? i : [i] }
end
# Worst case - all need operation
x.report("`Array()` non arrays") do
non_arrays.map {|i| Array(i) }
end
x.report("`[].flatten` non arrays") do
non_arrays.map {|i| [i].flatten }
end
x.report("`Array()` non arrays with check") do
non_arrays.map {|i| i.is_a?(Array) ? i : Array(i) }
end
x.report("`[]` non arrays with check") do
non_arrays.map {|i| i.is_a?(Array) ? i : [i] }
end
# Mixed case
x.report("`Array()` mixed") do
mixed.map {|i| Array(i) }
end
x.report("`[].flatten` mixed") do
mixed.map {|i| [i].flatten }
end
x.report("`Array()` mixed with check") do
mixed.map {|i| i.is_a?(Array) ? i : Array(i) }
end
x.report("`[]` mixed with check") do
mixed.map {|i| i.is_a?(Array) ? i : [i] }
end
x.compare!
end
Benchmark.ips &benchmark_lambda