Last active
January 20, 2016 04:41
-
-
Save marshluca/037f2a596e8a89173cc0 to your computer and use it in GitHub Desktop.
eval vs send
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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