Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods defined with define_method are slower to call #22

Open
janko opened this issue Feb 25, 2015 · 1 comment
Open

Methods defined with define_method are slower to call #22

janko opened this issue Feb 25, 2015 · 1 comment

Comments

@janko
Copy link

janko commented Feb 25, 2015

While define_method indeed defines methods slighly faster than module_eval, I think it should be noted that calling methods defined with define_method is slower than calling module_eval ones (because define_method creates a closure):

require "benchmark/ips"

object = Class.new {
  module_eval "def evaled_method; end"
  define_method(:defined_method) {}
}.new

Benchmark.ips do |x|
  x.report("module_eval")   { object.evaled_method }
  x.report("define_method") { object.defined_method }
  x.compare!
end
Calculating -------------------------------------
         module_eval    98.664k i/100ms
       define_method    93.809k i/100ms
-------------------------------------------------
         module_eval      6.794M (± 8.1%) i/s -     33.743M
       define_method      4.588M (± 6.7%) i/s -     22.889M

Comparison:
         module_eval:  6793763.3 i/s
       define_method:  4587570.1 i/s - 1.48x slower

I think you benefit more from a method being faster to call than faster to define.

@mateusdeap
Copy link
Member

I wonder in which category this should go into? Any opinions, @ashwinisukale ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants