Skip to content

Instantly share code, notes, and snippets.

@vasilakisfil
Created September 3, 2016 14:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasilakisfil/c8dbfa535064c11af9f957edc0238568 to your computer and use it in GitHub Desktop.
Save vasilakisfil/c8dbfa535064c11af9f957edc0238568 to your computer and use it in GitHub Desktop.
Ruby calling method performance
require 'benchmark/ips'
class Foo
def bar; end
end
foo = Foo.new
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
x.report('default: ') { foo.bar }
x.report('send: ') { foo.send(:bar) }
x.report('method call: ') { foo.method(:bar).call }
x.report('eval: ') { eval('foo.bar') }
x.compare!
end
#Warming up --------------------------------------
# default: 252.827k i/100ms
# send: 237.426k i/100ms
# method call: 149.763k i/100ms
# eval: 13.082k i/100ms
#Calculating -------------------------------------
# default: 9.707M (± 2.1%) i/s - 48.543M in 5.002960s
# send: 7.261M (± 4.0%) i/s - 36.326M in 5.012453s
# method call: 2.833M (± 5.2%) i/s - 14.227M in 5.039212s
# eval: 151.465k (± 4.4%) i/s - 758.756k in 5.020390s
#
#Comparison:
# default: : 9707100.7 i/s
# send: : 7260774.2 i/s - 1.34x slower
# method call: : 2832887.3 i/s - 3.43x slower
# eval: : 151464.9 i/s - 64.09x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment