-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_vs_public_send_vs_send.rb
More file actions
42 lines (33 loc) · 963 Bytes
/
method_vs_public_send_vs_send.rb
File metadata and controls
42 lines (33 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
require "benchmark/ips"
class Obj
def hello
end
end
obj = Obj.new
Benchmark.ips do |x|
x.compare!
x.report("method + call") do
obj.method(:hello).call
end
x.report("public_send") do
obj.public_send(:hello)
end
x.report("send") do
obj.send(:hello)
end
end
__DATA__
Sample Run with Ruby 2.5.0
Warming up --------------------------------------
method + call 157.777k i/100ms
public_send 246.986k i/100ms
send 267.647k i/100ms
Calculating -------------------------------------
method + call 2.571M (± 2.7%) i/s - 12.938M in 5.035042s
public_send 6.042M (± 3.3%) i/s - 30.379M in 5.033482s
send 7.656M (± 4.0%) i/s - 38.274M in 5.007630s
Comparison:
send: 7655632.3 i/s
public_send: 6041856.5 i/s - 1.27x slower
method + call: 2571436.8 i/s - 2.98x slower