Skip to content

Instantly share code, notes, and snippets.

@LTe
Created June 14, 2011 20:12
Show Gist options
  • Save LTe/1025761 to your computer and use it in GitHub Desktop.
Save LTe/1025761 to your computer and use it in GitHub Desktop.
require 'benchmark'
class InfoDesk
def flights
puts "flights"
end
def trains
puts "trains"
end
def hotels
puts "hotels"
end
end
class Sign
def initialize
@desk = InfoDesk.new
@methods = @desk.class.instance_methods(false)
end
def method_missing(name, *args)
return super unless @desk.respond_to? name
return "Out for lunch" if false
@desk.send(name, *args)
end
def respond_to_missing?(method, include_private)
@methods.include? method || super
end
end
class Sign2 < Sign
def respond_to_missing?(method, include_private)
false
end
def respond_to?(method)
@desk.respond_to?(method) || super
end
end
s = Sign.new
s2 = Sign2.new
Benchmark.bmbm do |x|
x.report("respond_to_missing?") { 999999.times{ s.respond_to? :trains } }
x.report("respond_to?") { 999999.times{ s2.respond_to? :trains } }
end
# RUBY 1.9.2-p0
#lite@lite-debian:~$ ruby respond.rb
#Rehearsal -------------------------------------------------------
#respond_to_missing? 0.800000 0.000000 0.800000 ( 0.835451)
#respond_to? 0.480000 0.000000 0.480000 ( 0.532853)
#---------------------------------------------- total: 1.280000sec
#
# user system total real
#respond_to_missing? 0.810000 0.000000 0.810000 ( 0.880678)
#respond_to? 0.470000 0.000000 0.470000 ( 0.497812)
# RUBY 1.8.7-p302
#lite@lite-debian:~$ ruby respond.rb
#Rehearsal -------------------------------------------------------
#respond_to_missing? 0.440000 0.000000 0.440000 ( 0.443771)
#respond_to? 0.970000 0.000000 0.970000 ( 0.967982)
#---------------------------------------------- total: 1.410000sec
#
# user system total real
#respond_to_missing? 0.430000 0.000000 0.430000 ( 0.437821)
#respond_to? 0.940000 0.000000 0.940000 ( 0.940762)
# JRUBY 1.5.5
#Rehearsal -------------------------------------------------------
#respond_to_missing? 0.573000 0.000000 0.573000 ( 0.475000)
#respond_to? 0.594000 0.000000 0.594000 ( 0.595000)
#---------------------------------------------- total: 1.167000sec
#
# user system total real
#respond_to_missing? 0.152000 0.000000 0.152000 ( 0.152000)
#respond_to? 0.283000 0.000000 0.283000 ( 0.283000)
# rbx rubinius 1.2.4dev
#Rehearsal -------------------------------------------------------
#respond_to_missing? 0.252015 0.000000 0.252015 ( 0.251117)
#respond_to? 0.264016 0.000000 0.264016 ( 0.265055)
#---------------------------------------------- total: 0.516031sec
#
# user system total real
#respond_to_missing? 0.200012 0.000000 0.200012 ( 0.198520)
#respond_to? 0.220014 0.000000 0.220014 ( 0.218376)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment