Skip to content

Instantly share code, notes, and snippets.

@Overbryd
Created April 1, 2016 20:38
Show Gist options
  • Save Overbryd/79dd9f187edd220e9927159172d8243a to your computer and use it in GitHub Desktop.
Save Overbryd/79dd9f187edd220e9927159172d8243a to your computer and use it in GitHub Desktop.
Ruby named params benchmark
require "benchmark/ips"
# Run instructions
#
# gem install benchmark-ips
# ruby benchmark.rb
def with_named_params(a:, b:)
a + b
end
def without_named_params(a, b)
a + b
end
def without_named_params_hash(h)
h[:a] + h[:b]
end
Benchmark.ips do |x|
x.report("named") do
with_named_params(a: 1, b: 2)
end
x.report("arguments") do
without_named_params(1, 2)
end
x.report("hash") do
without_named_params_hash(a: 1, b: 2)
end
x.compare!
end
@Overbryd
Copy link
Author

Overbryd commented Apr 1, 2016

Run on a MacBookPro11,5 Intel Core i7 2,5 GHz

Warming up --------------------------------------
               named   122.925k i/100ms
           arguments   135.041k i/100ms
                hash    77.049k i/100ms
Calculating -------------------------------------
               named      5.876M (± 6.5%) i/s -     29.256M
           arguments      7.742M (± 7.3%) i/s -     38.487M
                hash      1.545M (± 4.2%) i/s -      7.782M

Comparison:
           arguments:  7741807.6 i/s
               named:  5875621.0 i/s - 1.32x slower
                hash:  1545264.9 i/s - 5.01x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment