-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_vs_each_with_object.rb
52 lines (40 loc) · 1.22 KB
/
collect_vs_each_with_object.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
#!/usr/bin/env ruby
require 'benchmark'
# Make sure you `gem install benchmark-ips`
require 'benchmark/ips'
TIMES = [1, 16, 100, 1_000, 10_000]
benchmark_lambda = lambda do |x|
TIMES.each do |i|
array = Array.new(i) { |num| num }
x.report("collect - #{i}") do # fastest over all
array.collect do |item|
item.to_s
end
end
x.report("collect w/ skips + compact - #{i}") do # faster than each w/ obj + skipping
array.collect do |item|
next if item.odd?
item.to_s
end.compact
end
x.report("collect w/ skips + reject - #{i}") do # slower than compact for simple nils
array.collect do |item|
next if item.odd?
item.to_s
end.reject(&:nil?)
end
x.report("each_with_object([]) - #{i}") do # slower than collect
array.each_with_object([]) do |item, memo|
memo << item.to_s
end
end
x.report("each_with_object([]) w/ skips - #{i}") do # faster than w/o skips on each_with_object
array.each_with_object([]) do |item, memo|
next if item.odd?
memo << item.to_s
end
end
end
x.compare! # uncomment if you want comparisons between them all
end
Benchmark.ips(&benchmark_lambda); nil