Skip to content

Instantly share code, notes, and snippets.

@tobi
Created December 24, 2013 15:00
Show Gist options
  • Save tobi/8114443 to your computer and use it in GitHub Desktop.
Save tobi/8114443 to your computer and use it in GitHub Desktop.
require 'benchmark'
def regular(a, b, c)
[a, b, c]
end
def keyword(a: 1, b: 2, c: 3)
[a, b, c]
end
def hash(a)
[a[:a], a[:b], a[:c]]
end
number = 1000000
Benchmark.bm(4) do |bm|
bm.report("regular") { number.times { regular(4, 5, 6) } }
bm.report("keyword") { number.times { keyword(a: 4, b: 5, c: 6) } }
bm.report("hash") { number.times { hash(:a => 4, :b => 5, :c => 6) } }
end
tobi@box ~/tmp » ruby bm.rb
user system total real
regular 0.170000 0.000000 0.170000 ( 0.176344)
keyword 1.680000 0.010000 1.690000 ( 1.677325)
hash 0.790000 0.000000 0.790000 ( 0.794840)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment