-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_call_bench.rb
120 lines (100 loc) · 2.22 KB
/
method_call_bench.rb
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
require_relative '../../lib/ruby-on-speed'
RubyOnSpeed.benchmark 'Ruby:method_call - calling methods' do
class SampleBase
def self.call(num)
21 + num
end
def call(num)
21 + num
end
define_method(:defined_call) { |num| 21 + num }
eval('def evaled_call(num); 21 + num; end')
end
class SampleChild < SampleBase
end
class SampleOverride < SampleBase
def call(num)
super
end
def defined_call(num)
super
end
def evaled_call(num)
super
end
end
module SampleMixin
def call(num)
21 + num
end
end
class SampleWithMixin
include SampleMixin
end
sample_base = SampleBase.new
sample_child = SampleChild.new
sample_override = SampleOverride.new
sample_with_mixin = SampleWithMixin.new
sample_proc = proc { |num| 21 + num }
sample_lambda = ->(num) { 21 + num }
sample_unbound = sample_base.method(:call)
sample_class_unbound = SampleBase.method(:call)
code 'class method' do
SampleBase.call(21)
end
code 'method' do
sample_base.call(21)
end
code 'defined method' do
sample_base.defined_call(21)
end
code 'evaled method' do
sample_base.evaled_call(21)
end
code 'instance_exec*' do
sample_base.instance_exec(21) { |num| 21 + num }
end
code 'instance_exec' do
sample_base.instance_exec(21, &sample_proc)
end
code 'child method' do
sample_child.call(21)
end
code 'child defined method' do
sample_child.defined_call(21)
end
code 'child evaled method' do
sample_child.evaled_call(21)
end
code 'override method' do
sample_override.call(21)
end
code 'override defined method' do
sample_override.defined_call(21)
end
code 'override evaled method' do
sample_override.evaled_call(21)
end
code 'mixin method' do
sample_with_mixin.call(21)
end
code 'proc' do
sample_proc.call(21)
end
code 'lambda' do
sample_lambda.call(21)
end
code 'unbound method' do
sample_unbound.call(21)
end
code 'unbound class method' do
sample_class_unbound.call(21)
end
code 'public_send' do
sample_base.public_send(:call, 21)
end
code '__send__' do
sample_base.__send__(:call, 21)
end
end