Skip to content

Instantly share code, notes, and snippets.

@psylone
Created August 27, 2017 15:55
Show Gist options
  • Save psylone/0f09bc84e55f497c51c884735d9aefb6 to your computer and use it in GitHub Desktop.
Save psylone/0f09bc84e55f497c51c884735d9aefb6 to your computer and use it in GitHub Desktop.
require 'benchmark'
ITERATIONS = 1_000_000
srand(1)
s = (1..100).to_a
s += [nil] * 50
s.shuffle!
Benchmark.bm(10) do |bm|
bm.report('condition') do
ITERATIONS.times do
t = []
s.each do |e|
t.push if e
end
end
end
bm.report('compact') do
ITERATIONS.times do
t = []
s.each do |e|
t.push
end
t.compact
end
end
bm.report('compact!') do
ITERATIONS.times do
t = []
s.each do |e|
t.push
end
t.compact!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment