Skip to content

Instantly share code, notes, and snippets.

@etehtsea
Last active October 21, 2015 12:52
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 etehtsea/b3ffdc4ff8c14e72e674 to your computer and use it in GitHub Desktop.
Save etehtsea/b3ffdc4ff8c14e72e674 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'monitor'
class MonitorCounter
def initialize
@counter = 0
# No idea why this doesn't work inside the class declaration
extend(MonitorMixin)
end
def incr
synchronize { @counter = @counter + 1 }
end
def value
@counter
end
end
class StraightCounter
def initialize
@counter = 0
end
def incr
@counter = @counter + 1
end
def value
@counter
end
end
monitored = MonitorCounter.new
straight = StraightCounter.new
Benchmark.ips do |x|
x.config(time: 10, warmup: 20)
x.report("Monitor") { monitored.incr }
x.report("Straght") { straight.incr }
end
monitored = MonitorCounter.new
straight = StraightCounter.new
TIMES = 10_000_000
Benchmark.bmbm do |x|
x.report("Straight") { TIMES.times { straight.incr } }
x.report("Monitored") { TIMES.times { monitored.incr } }
end
# Calculating -------------------------------------
# Monitor 109.513k i/100ms
# Straght 235.768k i/100ms
# -------------------------------------------------
# Monitor 1.899M (± 5.4%) i/s - 18.946M
# Straght 11.272M (± 7.3%) i/s - 111.990M
# Rehearsal ---------------------------------------------
# Straight 0.930000 0.050000 0.980000 ( 0.638000)
# Monitored 5.890000 0.080000 5.970000 ( 5.239000)
# ------------------------------------ total: 6.950000sec
#
# user system total real
# Straight 0.590000 0.000000 0.590000 ( 0.588000)
# Monitored 5.090000 0.030000 5.120000 ( 5.086000)
# true
# [20000000, 20000000]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment