Skip to content

Instantly share code, notes, and snippets.

@thumblemonks
Created October 27, 2009 02:56
Show Gist options
  • Save thumblemonks/219259 to your computer and use it in GitHub Desktop.
Save thumblemonks/219259 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Foo
def a; true; end
end
n = 1_000_000
Benchmark.bm(30) do | b |
instance = Foo.new
b.report( "a simple method call" ) do
n.times { instance.a }
end
instance = Foo.new
block = lambda { true }
b.report( "instance_eval with block" ) do
n.times { instance.instance_eval( &block ) }
end
instance = Foo.new
string = "true"
b.report( "instance_eval with string" ) do
n.times { instance.instance_eval( string ) }
end
method = Foo.instance_method( :a )
instance = Foo.new
bound_method = method.bind( instance )
b.report( "bound method" ) do
n.times { bound_method.call }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment