Skip to content

Instantly share code, notes, and snippets.

@rkh
Created December 1, 2011 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkh/1415044 to your computer and use it in GitHub Desktop.
Save rkh/1415044 to your computer and use it in GitHub Desktop.
# Original Benchmark used to consider whether to keep define_method or switch
# to instance_exec for Sinatra 1.3.0.
#
# == Ruby 1.8.7
# instan_exec 0.650000 0.000000 0.650000 ( 0.650820)
# instan_eval 0.620000 0.000000 0.620000 ( 0.621846)
# bind.call 0.140000 0.010000 0.150000 ( 0.141961)
#
# == Ruby 1.9.2
# user system total real
# instan_exec 0.830000 0.010000 0.840000 ( 0.851241)
# instan_eval 0.800000 0.000000 0.800000 ( 0.811791)
# bind.call 0.110000 0.000000 0.110000 ( 0.119343)
#
# == Ruby 1.9.3
# user system total real
# instan_exec 0.190000 0.010000 0.200000 ( 0.338498)
# instan_eval 0.170000 0.000000 0.170000 ( 0.177155)
# bind.call 0.130000 0.000000 0.130000 ( 0.133703)
#
# == Rubinius 2.0.0dev
# user system total real
# instan_exec 0.330260 0.004597 0.334857 ( 0.350686)
# instan_eval 0.311549 0.011523 0.323072 ( 0.917773)
# bind.call 0.096223 0.000337 0.096560 ( 0.097297)
#
# == JRuby 1.6.5
# instan_exec 5.736000 0.000000 5.736000 ( 5.736000)
# instan_eval 2.825000 0.000000 2.825000 ( 2.825000)
# bind.call 0.154000 0.000000 0.154000 ( 0.153000)
require 'benchmark'
class DSL
def add(x) x + 10 end
end
num, value, block = 50000, 20, proc { add value }
DSL.send(:define_method, :bind_me, &block)
undbound_method = DSL.instance_method(:bind_me)
DSL.send(:remove_method, :bind_me)
Benchmark.bmbm do |x|
x.report("instan_exec") { num.times { DSL.new.instance_exec(&block) }}
x.report("instan_eval") { num.times { DSL.new.instance_eval(&block) }}
x.report("bind.call") { num.times { undbound_method.bind(DSL.new).call }}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment