Skip to content

Commit 7708a6c

Browse files
committed
Tiny fixes for #40
Closes #40
1 parent 47dcf13 commit 7708a6c

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

README.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,6 @@ 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-
6139

6240
### General
6341

@@ -117,6 +95,30 @@ Comparison:
11795
module_eval with string: 1129.7 i/s - 1.19x slower
11896
```
11997

98+
#### Method Invocation
99+
100+
##### `call` vs `send` vs `method_missing` [code](code/method/call-vs-send-vs-method_missing.rb)
101+
102+
```
103+
$ ruby -v code/method/call-vs-send-vs-method_missing.rb
104+
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
105+
106+
Calculating -------------------------------------
107+
call 115.094k i/100ms
108+
send 105.258k i/100ms
109+
method_missing 100.762k i/100ms
110+
-------------------------------------------------
111+
call 3.811M (± 5.9%) i/s - 18.991M
112+
send 3.244M (± 7.2%) i/s - 16.210M
113+
method_missing 2.729M (± 9.8%) i/s - 13.401M
114+
115+
Comparison:
116+
call: 3811183.4 i/s
117+
send: 3244239.1 i/s - 1.17x slower
118+
method_missing: 2728893.0 i/s - 1.40x slower
119+
```
120+
121+
120122
### Array
121123

122124
##### `Array#bsearch` vs `Array#find` [code](code/array/bsearch-vs-find.rb)
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
require 'benchmark/ips'
1+
require "benchmark/ips"
22

33
class MethodCall
4+
def method
5+
end
46

5-
def method
6-
end
7-
8-
def method_missing(mt,*args)
9-
method
10-
end
7+
def method_missing(_method,*args)
8+
method
9+
end
1110
end
1211

13-
def fast
14-
mt = MethodCall.new
15-
mt.method
12+
def fastest
13+
method = MethodCall.new
14+
method.method
1615
end
1716

1817
def slow
19-
mt = MethodCall.new
20-
mt.send(:method)
18+
method = MethodCall.new
19+
method.send(:method)
2120
end
2221

23-
def slow_1
24-
mt = MethodCall.new
25-
mt.youknow
22+
def slowest
23+
method = MethodCall.new
24+
method.not_exist
2625
end
2726

2827
Benchmark.ips do |x|
29-
x.report("call") { fast }
30-
x.report("send") { slow }
31-
x.report("method_missing") { slow_1 }
28+
x.report("call") { fastest }
29+
x.report("send") { slow }
30+
x.report("method_missing") { slowest }
3231
x.compare!
33-
end
32+
end

0 commit comments

Comments
 (0)