File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -540,6 +540,27 @@ Enumerable#sort_by (Symbol#to_proc): 25916.1 i/s
540540 Enumerable#sort: 14018.3 i/s - 1.85x slower
541541```
542542
543+ ##### ` Enumerable#inject Symbol ` vs ` Enumerable#inject Proc ` [ code] ( code/enumerable/inject-symbol-vs-block.rb )
544+
545+ Of note, ` to_proc ` for 1.8.7 is considerable slower than the block format
546+
547+ ```
548+ $ ruby -v code/enumerable/inject-sum-vs-block.rb
549+ ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin14]
550+ Warming up --------------------------------------
551+ inject symbol 1.893k i/100ms
552+ inject to_proc 1.583k i/100ms
553+ inject block 1.390k i/100ms
554+ Calculating -------------------------------------
555+ inject symbol 19.001k (± 3.8%) i/s - 96.543k
556+ inject to_proc 15.958k (± 3.5%) i/s - 80.733k
557+ inject block 14.063k (± 3.9%) i/s - 70.890k
558+
559+ Comparison:
560+ inject symbol: 19001.5 i/s
561+ inject to_proc: 15958.3 i/s - 1.19x slower
562+ inject block: 14063.1 i/s - 1.35x slower
563+ ```
543564
544565### Hash
545566
Original file line number Diff line number Diff line change 1+ require "rubygems"
2+ require "benchmark/ips"
3+
4+ ARRAY = ( 1 ..1000 ) . to_a
5+
6+ def fastest
7+ ARRAY . inject ( :+ )
8+ end
9+
10+ def fast
11+ ARRAY . inject ( &:+ )
12+ end
13+
14+ def slow
15+ ARRAY . inject { |a , i | a + i }
16+ end
17+
18+ Benchmark . ips do |x |
19+ x . report ( 'inject symbol' ) { fastest }
20+ x . report ( 'inject to_proc' ) { fast }
21+ x . report ( 'inject block' ) { slow }
22+
23+ x . compare!
24+ end
You can’t perform that action at this time.
0 commit comments