Skip to content

Instantly share code, notes, and snippets.

@marionzualo
Last active November 7, 2015 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marionzualo/7eb04ae97fa1c5bdef4b to your computer and use it in GitHub Desktop.
Save marionzualo/7eb04ae97fa1c5bdef4b to your computer and use it in GitHub Desktop.
Benchmark keyword arguments, normal arguments and an options hash in Ruby 2.1.5 and 2.2.3. Background: Koichi's talk on the evolution of keyword parameters at Ruby Conf PT - https://www.youtube.com/watch?v=qNaXZ1VWuvA. Inspired by https://gist.github.com/postmodern/5274138
require 'benchmark/ips'
def keyword(a:1,b:2,c:3)
a + b + c
end
def normal(a=1,b=2,c=3)
a + b + c
end
def options(params = {})
a = params.fetch(:a, 1)
b = params.fetch(:b, 2)
c = params.fetch(:c, 3)
a + b + c
end
Benchmark.ips do |bm|
bm.report("keyword arguments") { keyword(a:7,b:8,c:9) }
bm.report("normal arguments") { normal(7,8,9) }
bm.report("options hash") { options(a: 7,b: 8,c: 9) }
bm.compare!
end
# Ruby 2.1.5
# Calculating -------------------------------------
# keyword arguments 22.074k i/100ms
# normal arguments 70.955k i/100ms
# options hash 31.267k i/100ms
# -------------------------------------------------
# keyword arguments 319.329k (± 3.6%) i/s - 1.611M
# normal arguments 3.340M (± 4.4%) i/s - 16.674M
# options hash 545.974k (± 4.1%) i/s - 2.751M
# Comparison:
# normal arguments: 3339916.0 i/s
# options hash: 545974.2 i/s - 6.12x slower
# keyword arguments: 319329.1 i/s - 10.46x slower
# Ruby 2.2.3
# Calculating -------------------------------------
# keyword arguments 66.866k i/100ms
# normal arguments 69.380k i/100ms
# options hash 32.776k i/100ms
# -------------------------------------------------
# keyword arguments 2.742M (± 3.6%) i/s - 13.708M
# normal arguments 3.316M (± 4.1%) i/s - 16.582M
# options hash 570.139k (± 3.6%) i/s - 2.852M
# Comparison:
# normal arguments: 3315957.5 i/s
# keyword arguments: 2741896.6 i/s - 1.21x slower
# options hash: 570139.0 i/s - 5.82x slower
# This example supplies no arguments so that the default values are used
Benchmark.ips do |bm|
bm.report("keyword arguments") { keyword }
bm.report("normal arguments") { normal }
bm.report("options hash") { options }
bm.compare!
end
# Ruby 2.1.5
# Calculating -------------------------------------
# keyword arguments 59.167k i/100ms
# normal arguments 70.488k i/100ms
# options hash 54.960k i/100ms
# -------------------------------------------------
# keyword arguments 2.031M (± 3.6%) i/s - 10.177M
# normal arguments 3.355M (± 3.5%) i/s - 16.776M
# options hash 1.489M (± 3.1%) i/s - 7.475M
# Comparison:
# normal arguments: 3354831.4 i/s
# keyword arguments: 2030610.2 i/s - 1.65x slower
# options hash: 1489103.3 i/s - 2.25x slower
# Ruby 2.2.3
# Calculating -------------------------------------
# keyword arguments 70.215k i/100ms
# normal arguments 70.195k i/100ms
# options hash 56.138k i/100ms
# -------------------------------------------------
# keyword arguments 3.032M (± 3.6%) i/s - 15.166M
# normal arguments 3.195M (± 4.1%) i/s - 16.004M
# options hash 1.704M (± 7.6%) i/s - 8.421M
# Comparison:
# normal arguments: 3195331.5 i/s
# keyword arguments: 3031580.0 i/s - 1.05x slower
# options hash: 1704400.8 i/s - 1.87x slower
require 'benchmark/ips'
def keyword(a:,b:,c:)
a + b + c
end
def normal(a,b,c)
a + b + c
end
def options(params)
a = params.fetch(:a)
b = params.fetch(:b)
c = params.fetch(:c)
a + b + c
end
Benchmark.ips do |bm|
bm.report("keyword arguments") { keyword(a:7,b:8,c:9) }
bm.report("normal arguments") { normal(7,8,9) }
bm.report("options hash") { options(a: 7,b: 8,c: 9) }
bm.compare!
end
# Ruby 2.1.5
# Calculating -------------------------------------
# keyword arguments 21.624k i/100ms
# normal arguments 73.576k i/100ms
# options hash 32.015k i/100ms
# -------------------------------------------------
# keyword arguments 318.974k (± 3.7%) i/s - 1.600M
# normal arguments 3.541M (± 3.4%) i/s - 17.732M
# options hash 557.704k (± 3.1%) i/s - 2.817M
# Comparison:
# normal arguments: 3541085.7 i/s
# options hash: 557704.3 i/s - 6.35x slower
# keyword arguments: 318974.2 i/s - 11.10x slower
# Ruby 2.2.3
# Calculating -------------------------------------
# keyword arguments 65.847k i/100ms
# normal arguments 69.809k i/100ms
# options hash 33.905k i/100ms
# -------------------------------------------------
# keyword arguments 2.856M (± 4.1%) i/s - 14.289M
# normal arguments 3.839M (± 4.5%) i/s - 19.197M
# options hash 566.961k (± 4.5%) i/s - 2.848M
# Comparison:
# normal arguments: 3839040.8 i/s
# keyword arguments: 2855939.8 i/s - 1.34x slower
# options hash: 566961.1 i/s - 6.77x slower
@pedrocarrico
Copy link

👍

@marionzualo
Copy link
Author

@pedrocarrico I swapped standard lib's benchmark for benchmark-ips. Everything reads more clearly now!

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