Skip to content

Instantly share code, notes, and snippets.

@jasonroelofs
Created March 23, 2009 20:31
Show Gist options
  • Save jasonroelofs/83757 to your computer and use it in GitHub Desktop.
Save jasonroelofs/83757 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'rubygems'
require 'publisher'
COUNT = 10_000_000
class Eventer
extend Publisher
def initialize
@ev_one_count = 0
end
has_events :ev_one
def ev_one
@ev_one_count += 1
end
end
class Firer
extend Publisher
can_fire :ev_one
def run_test
COUNT.times { fire :ev_one }
end
end
class Caller
def initialize(eventer)
@eventer = eventer
end
def run_test
COUNT.times { @eventer.ev_one }
end
end
Benchmark.bm(20) do |x|
x.report("Publisher events") { f = Firer.new; ev = Eventer.new; f.run_test }
x.report("Calling straight") { ev = Eventer.new; c = Caller.new(ev); c.run_test }
end
roelofs@trillian ~/tmp $ ruby publisher_test.rb
user system total real
Publisher events 36.870000 0.010000 36.880000 ( 37.352613)
Calling straight 6.740000 0.000000 6.740000 ( 6.835743)
roelofs@trillian ~/tmp $ ruby19 -v
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]
roelofs@trillian ~/tmp $ ruby19 publisher_test.rb
user system total real
Publisher events 13.720000 0.080000 13.800000 ( 15.669221)
Calling straight 3.100000 0.020000 3.120000 ( 3.532273)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment