-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-vs-zip.rb
42 lines (34 loc) · 862 Bytes
/
map-vs-zip.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
require 'benchmark/ips'
require 'benchmark/memory'
require 'colorize'
arrays = {
small: (1..4).to_a,
medium: (1..1000).to_a,
big: (1..50000).to_a,
}
arrays.each do |size, array|
puts "=== #{size} ===".blue.bold
Benchmark.ips do |x|
x.report(".map { |id| [id] }:") do
array.map { |id| [id] }
end
x.report(".zip:") do
array.zip
end
x.report(".each_with_object([]) {|id, object| object << [id]}:") do
array.each_with_object([]) {|id, object| object << [id]}
end
x.compare! order: :baseline
end
Benchmark.memory do |x|
x.report(".map { |id| [id] }:") do
array.map { |id| [id] }
end
x.report(".zip:") do
array.zip
end
x.report(".each_with_object([]) {|id, object| object << [id]}:") do
array.each_with_object([]) {|id, object| object << [id]}
end
end
end