Skip to content

Commit 47dcf13

Browse files
leosaullvJuanitoFatas
authored andcommitted
Add benchmark for method invocations
1 parent 1dd59ce commit 47dcf13

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ end
3636

3737
Idioms
3838
------
39+
### Method
40+
#### call vs send vs method_missing [code](code/method/call-vs-send-vs-method_missing.rb)
41+
42+
```
43+
$ ruby -v code/method/call-vs-send-vs-method_missing.rb
44+
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin14.0]
45+
46+
Calculating -------------------------------------
47+
call 72.324k i/100ms
48+
send 70.775k i/100ms
49+
method_missing 63.545k i/100ms
50+
-------------------------------------------------
51+
call 2.566M (± 9.6%) i/s - 12.729M
52+
send 2.527M (± 9.3%) i/s - 12.527M
53+
method_missing 1.924M (± 8.8%) i/s - 9.595M
54+
55+
Comparison:
56+
call: 2566314.9 i/s
57+
send: 2527436.5 i/s - 1.02x slower
58+
method_missing: 1923544.5 i/s - 1.33x slower
59+
```
60+
3961

4062
### General
4163

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'benchmark/ips'
2+
3+
class MethodCall
4+
5+
def method
6+
end
7+
8+
def method_missing(mt,*args)
9+
method
10+
end
11+
end
12+
13+
def fast
14+
mt = MethodCall.new
15+
mt.method
16+
end
17+
18+
def slow
19+
mt = MethodCall.new
20+
mt.send(:method)
21+
end
22+
23+
def slow_1
24+
mt = MethodCall.new
25+
mt.youknow
26+
end
27+
28+
Benchmark.ips do |x|
29+
x.report("call") { fast }
30+
x.report("send") { slow }
31+
x.report("method_missing") { slow_1 }
32+
x.compare!
33+
end

0 commit comments

Comments
 (0)