Skip to content

Instantly share code, notes, and snippets.

@ElMassimo
Forked from nickcherry/array_splat_benchmark.rb
Last active August 29, 2015 14:14
Show Gist options
  • Save ElMassimo/b403c1c2dd2c30957352 to your computer and use it in GitHub Desktop.
Save ElMassimo/b403c1c2dd2c30957352 to your computer and use it in GitHub Desktop.
require 'benchmark'
ITERATIONS = 1_000_000
def rename(source, dest)
{ source: source, dest: dest }
end
def literal_work
ITERATIONS.times do
rename *[:_id, :oid]
end
end
def dynamic_work
rename_field = []
rename_field << :_id
rename_field << :oid
ITERATIONS.times do
rename *rename_field
end
end
Benchmark.bm do |bm|
bm.report('splatted array literal') do
literal_work
end
bm.report('splatted dynamic array') do
dynamic_work
end
bm.report 'splatted array literal as eval string' do
eval 'literal_work'
end
bm.report('splatted dynamic array as eval string') do
eval 'dynamic_work'
end
end
# Results user system total real
splatted array literal 0.550000 0.020000 0.570000 (0.562727)
splatted dynamic array 0.540000 0.020000 0.560000 (0.565917)
splatted array literal as eval string 0.550000 0.020000 0.570000 (0.564907)
splatted dynamic array as eval string 0.500000 0.020000 0.520000 (0.521945)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment