Created
January 23, 2015 02:15
-
-
Save nickcherry/2a94b8df721b573019d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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