File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,23 @@ Comparison:
188
188
Kernel loop: 0.2 i/s - 2.41x slower
189
189
```
190
190
191
+ ##### ` ancestors.include? ` vs ` <= ` [ code] ( code/general/inheritance-check.rb )
192
+
193
+ ```
194
+ $ ruby -vW0 code/general/inheritance-check.rb
195
+ ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
196
+ Warming up --------------------------------------
197
+ less than or equal 66.992k i/100ms
198
+ ancestors.include? 16.943k i/100ms
199
+ Calculating -------------------------------------
200
+ less than or equal 1.250M (± 6.4%) i/s - 6.230M in 5.006896s
201
+ ancestors.include? 192.603k (± 4.8%) i/s - 965.751k in 5.025917s
202
+
203
+ Comparison:
204
+ less than or equal: 1249606.0 i/s
205
+ ancestors.include?: 192602.9 i/s - 6.49x slower
206
+ ```
207
+
191
208
#### Method Invocation
192
209
193
210
##### ` call ` vs ` send ` vs ` method_missing ` [ code] ( code/method/call-vs-send-vs-method_missing.rb )
Original file line number Diff line number Diff line change
1
+ require 'benchmark/ips'
2
+
3
+ # You may ask: 'Is there a project that still using `ancestors.include?`?'
4
+ # By quick searching, I found the following popular repositories are still using it:
5
+ # - rake
6
+ # - https://github.com/ruby/rake/blob/7d0c08fe4e97083a92d2c8fc740cb421fd062117/lib/rake/task_manager.rb#L28
7
+ # - warden
8
+ # - https://github.com/hassox/warden/blob/090ed153dbd2f5bf4a1ca672b3018877e21223a4/lib/warden/strategies.rb#L16
9
+ # - metasploit-framework
10
+ # - https://github.com/rapid7/metasploit-framework/blob/cac890a797d0d770260074dfe703eb5cfb63bd46/lib/msf/core/payload_set.rb#L239
11
+ # - https://github.com/rapid7/metasploit-framework/blob/cb82015c8782280d964e222615b54c881bd36bbe/lib/msf/core/exploit.rb#L1440
12
+ # - hanami
13
+ # - https://github.com/hanami/hanami/blob/506a35e5262939eb4dce9195ade3268e19928d00/lib/hanami/components/routes_inspector.rb#L54
14
+ # - https://github.com/hanami/hanami/blob/aec069b602c772e279aa0a7f48d1a04d01756ee3/lib/hanami/configuration.rb#L114
15
+ raise unless Object . ancestors . include? ( Kernel )
16
+ raise unless ( Object <= Kernel )
17
+
18
+ def fast
19
+ ( Class <= Class )
20
+ ( Class <= Module )
21
+ ( Class <= Object )
22
+ ( Class <= Kernel )
23
+ ( Class <= BasicObject )
24
+ end
25
+
26
+ def slow
27
+ Class . ancestors . include? ( Class )
28
+ Class . ancestors . include? ( Module )
29
+ Class . ancestors . include? ( Object )
30
+ Class . ancestors . include? ( Kernel )
31
+ Class . ancestors . include? ( BasicObject )
32
+ end
33
+
34
+ Benchmark . ips do |x |
35
+ x . report ( 'less than or equal' ) { fast }
36
+ x . report ( 'ancestors.include?' ) { slow }
37
+ x . compare!
38
+ end
You can’t perform that action at this time.
0 commit comments