Skip to content

Instantly share code, notes, and snippets.

@isaiah
Created May 8, 2013 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaiah/5539458 to your computer and use it in GitHub Desktop.
Save isaiah/5539458 to your computer and use it in GitHub Desktop.
Ruby dynamic calls benchmark
n = 100000
# taken from [here](http://blog.lojic.com/2008/12/22/ruby-dynamic-method-invocation-performance/),
# but the original one use the same string all the time, which doesn't reflect real world situation
bm(12) {|x|
x.report("orig") { n.times { test = "stormy weather"; test.length } }
x.report("call") { n.times { test = "stormy weather"; m = test.method(:length); m.call } }
x.report("send") { n.times { test = "stormy weather"; test.send(:length) } }
x.report("eval") { n.times { test = "stormy weather"; eval "test.length" } }
}
# user system total real
# orig 0.020000 0.000000 0.020000 ( 0.017626)
# call 0.080000 0.010000 0.090000 ( 0.093148)
# send 0.030000 0.000000 0.030000 ( 0.023334)
# eval 0.990000 0.000000 0.990000 ( 1.005901)
#
# we can see method.call is more than 5 times slower
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment