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
Benchmark.bm do |bm|
bm.report('splatted array literal') do
iterations.times do
rename *[:_id, :oid]
end
end
bm.report('splatted dynamic array') do
rename_field = []
rename_field.push :_id
rename_field.push :oid
iterations.times do
rename *rename_field
end
end
bm.report 'splatted array literal as eval string' do
eval_string = "rename *[:_id, :oid]"
iterations.times do
eval(eval_string, binding)
end
end
bm.report('splatted dynamic array as eval string') do
eval_string = "r = []; r.push(:_id); r.push(:oid); rename *r"
iterations.times do
eval(eval_string, binding)
end
end
end
# Results user system total real
# splatted array literal 0.750000 0.020000 0.770000 ( 0.777989)
# splatted dynamic array 0.630000 0.020000 0.650000 ( 0.655612)
# splatted array literal as eval string 10.870000 0.120000 10.990000 ( 11.116578)
# splatted dynamic array as eval string 17.820000 0.200000 18.020000 ( 18.261153)
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.