Created
September 5, 2020 13:32
-
-
Save Thomascountz/71bd837ba1ab05e7118ccc06b7d2058c to your computer and use it in GitHub Desktop.
Benchmark using ARGS constant for nil method arguments
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/ips' | |
Benchmark.ips do |benchmark| | |
benchmark.config(time: 5, warmup: 2) | |
benchmark.report("keyword-args") do | |
def foo(a: nil, b: nil, c: nil, d: nil, e: nil) | |
bar(a: a, b: b, c: c, d: d, e: e) | |
end | |
def bar(a: nil, b: nil, c: nil, d: nil, e: nil) | |
e | |
end | |
foo() | |
end | |
benchmark.report("opts-hash") do | |
def foo(args={}) | |
bar(args) | |
end | |
def bar(args={}) | |
args[:e] | |
end | |
foo() | |
end | |
ARGS = {}.freeze | |
benchmark.report("opts-hash-constant") do | |
def foo(args=ARGS) | |
bar(args) | |
end | |
def bar(args=ARGS) | |
args[:e] | |
end | |
foo() | |
end | |
benchmark.report("opts-splat") do | |
def foo(**args) | |
bar(args) | |
end | |
def bar(args) | |
args[:e] | |
end | |
foo() | |
end | |
benchmark.report("caller-splat") do | |
def foo(**args) | |
bar(**args) | |
end | |
def bar(**args) | |
args[:e] | |
end | |
foo(**{}) | |
end | |
benchmark.compare! | |
end | |
### RESULTS ### | |
# Warming up -------------------------------------- | |
# keyword-args 115.618k i/100ms | |
# opts-hash 113.442k i/100ms | |
# opts-hash-constant 116.818k i/100ms | |
# opts-splat 111.250k i/100ms | |
# caller-splat 94.542k i/100ms | |
# Calculating ------------------------------------- | |
# keyword-args 1.156M (± 1.2%) i/s - 5.781M in 5.002661s | |
# opts-hash 1.123M (± 1.3%) i/s - 5.672M in 5.050629s | |
# opts-hash-constant 1.162M (± 0.9%) i/s - 5.841M in 5.028786s | |
# opts-splat 1.110M (± 0.9%) i/s - 5.562M in 5.011539s | |
# caller-splat 948.735k (± 1.1%) i/s - 4.822M in 5.082830s | |
# | |
# Comparison: | |
# opts-hash-constant: 1161595.6 i/s | |
# keyword-args: 1155730.1 i/s - same-ish: difference falls within error | |
# opts-hash: 1123228.9 i/s - 1.03x (± 0.00) slower | |
# opts-splat: 1110022.7 i/s - 1.05x (± 0.00) slower | |
# caller-splat: 948734.7 i/s - 1.22x (± 0.00) slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment