Skip to content

Instantly share code, notes, and snippets.

@nickcherry
Created January 23, 2015 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nickcherry/2a94b8df721b573019d8 to your computer and use it in GitHub Desktop.
Save nickcherry/2a94b8df721b573019d8 to your computer and use it in GitHub Desktop.
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