Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.