Skip to content

Instantly share code, notes, and snippets.

@localshred
Created October 18, 2012 20:37
Show Gist options
  • Save localshred/3914546 to your computer and use it in GitHub Desktop.
Save localshred/3914546 to your computer and use it in GitHub Desktop.
I was wondering how fast the different dynamic method dispatching options in ruby are. After doing this, I guess I shouldn't be surprised. My assumption is that the method object call is slower since we're actually instantiating an object..
require 'benchmark'
class TestMethodCall
def test
100 + 100
end
end
Benchmark.benchmark("Dynamic method calling\n") do |x|
instance = TestMethodCall.new
x.report('__send__') do
1_000_000.times { instance.__send__(:test) }
end
x.report('method object call') do
1_000_000.times { instance.method(:test).call }
end
end
Dynamic method calling
__send__ 0.220000 0.000000 0.220000 ( 0.234082)
method object call 0.900000 0.060000 0.960000 ( 1.474704)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment