Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Created January 6, 2018 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckremes/3d0cc0f6651d3915e89e74133fac56c0 to your computer and use it in GitHub Desktop.
Save chuckremes/3d0cc0f6651d3915e89e74133fac56c0 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
class Ak
def initialize(to:)
@to = to
end
def read(a:, b:, c:, d: nil)
@to.read(a: a, b: b, c: c, d: d)
end
end
class Bk
def initialize(to:)
@to = to
end
def read(a:, b:, c:, d: nil)
@to.read(a, b, c, d)
end
end
class A
def initialize(to)
@to = to
end
def read(a, b, c, d=nil)
@to.read(a, b, c, d)
end
end
class E
def read(a, b, c, d)
#Time.at(a.to_f + b.to_f + c.to_f + d.to_f)
end
end
class Ek
def read(a:, b:, c:, d:)
#Time.at(a.to_f + b.to_f + c.to_f + d.to_f)
end
end
with_words = Ak.new(to: Ak.new(to: Ak.new(to: Bk.new(to: E.new))))
no_words = A.new(A.new(A.new(A.new(E.new))))
none_one_level = E.new
keys_one_level = Ek.new
Benchmark.ips do |x|
# Configure the number of seconds used during
# the warmup phase (default 2) and calculation phase (default 5)
x.config(:time => 15, :warmup => 5)
x.report("keywords") do |times|
i = 0
while i < times
with_words.read(a: 3, b: 3**7.5, c: (3**7.5 % 3))
i += 1
end
end
x.report("keywords, 1-level deep") do |times|
i = 0
while i < times
keys_one_level.read(a: 3, b: 3**7.5, c: (3**7.5 % 3), d: nil)
i += 1
end
end
x.report("nokeywords") do |times|
i = 0
while i < times
no_words.read(3, 3**7.5, (3**7.5 % 3))
i += 1
end
end
x.report("nokeywords, 1-level deep") do |times|
i = 0
while i < times
none_one_level.read(3, 3**7.5, (3**7.5 % 3), nil)
i += 1
end
end
# Compare the iterations per second of the various reports!
x.compare!
end
Charless-Air:ruby-io cremes$ ruby benchmarks/keywords_vs_none.rb
Warming up --------------------------------------
keywords 593.000 i/100ms
keywords, 1-level deep
3.464k i/100ms
nokeywords 35.944k i/100ms
nokeywords, 1-level deep
45.310k i/100ms
Calculating -------------------------------------
keywords 6.797k (± 7.3%) i/s - 101.403k
keywords, 1-level deep
37.558k (± 9.5%) i/s - 557.704k
nokeywords 1.025M (± 9.7%) i/s - 15.168M
nokeywords, 1-level deep
1.862M (±10.1%) i/s - 27.413M
Comparison:
nokeywords, 1-level deep: 1862414.6 i/s
nokeywords: 1024647.5 i/s - 1.82x slower
keywords, 1-level deep: 37558.4 i/s - 49.59x slower
keywords: 6797.1 i/s - 274.00x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment