Skip to content

Instantly share code, notes, and snippets.

@marshluca
Last active January 20, 2016 04:41
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 marshluca/037f2a596e8a89173cc0 to your computer and use it in GitHub Desktop.
Save marshluca/037f2a596e8a89173cc0 to your computer and use it in GitHub Desktop.
eval vs send
require 'benchmark'
require 'active_support/inflector'
class Foo
def self.bar
end
end
def with_eval
eval "Foo.bar"
end
def with_meta
(_class, _method) = "Foo.bar".split(".")
_class.constantize.send(_method)
end
Benchmark.bmbm do |x|
x.report("eval:") do
10000.times { |i| with_eval }
end
x.report("meta:") do
10000.times { |i| with_meta }
end
end
$ ruby eval_benchmark.rb
Rehearsal -----------------------------------------
eval: 0.080000 0.000000 0.080000 ( 0.085657)
meta: 0.030000 0.000000 0.030000 ( 0.025313)
-------------------------------- total: 0.110000sec
user system total real
eval: 0.080000 0.000000 0.080000 ( 0.079746)
meta: 0.020000 0.000000 0.020000 ( 0.024821)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment