fastruby / fastruby/fast-ruby

Methods defined with `define_method` are slower to call

Đang mở
#22 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Suggestion
Ngôn ngữ chính
Ruby
Star
5.7k
Fork
371
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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):

``` rb
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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.