File tree Expand file tree Collapse file tree 2 files changed +15
-15
lines changed Expand file tree Collapse file tree 2 files changed +15
-15
lines changed Original file line number Diff line number Diff line change @@ -384,18 +384,18 @@ Comparison:
384384
385385```
386386$ ruby -v code/hash/fetch-vs-fetch-with-block.rb
387- ruby 2.2.0p0 (2014-12-25 revision 49005 ) [x86_64-darwin14]
387+ ruby 2.2.1p85 (2015-02-26 revision 49769 ) [x86_64-darwin14]
388388
389389Calculating -------------------------------------
390- Hash#fetch + arg 15.650k i/100ms
391- Hash#fetch + block 130.271k i/100ms
390+ Hash#fetch + block 139.880k i/100ms
391+ Hash#fetch + arg 119.645k i/100ms
392392-------------------------------------------------
393- Hash#fetch + arg 184.562k (± 5.2 %) i/s - 923.350k
394- Hash#fetch + block 5.880M (± 7.5 %) i/s - 29.311M
393+ Hash#fetch + block 6.116M (± 8.9 %) i/s - 30.354M
394+ Hash#fetch + arg 4.473M (± 9.9 %) i/s - 22.134M
395395
396396Comparison:
397- Hash#fetch + block: 5880209.2 i/s
398- Hash#fetch + arg: 184562 .0 i/s - 31.86x slower
397+ Hash#fetch + block: 6116059.5 i/s
398+ Hash#fetch + arg: 4472636 .0 i/s - 1.37x slower
399399```
400400
401401##### ` Hash#each_key ` instead of ` Hash#keys.each ` [ code] ( code/hash/keys-each-vs-each_key.rb )
Original file line number Diff line number Diff line change 1- require ' benchmark/ips'
1+ require " benchmark/ips"
22
3- HASH = { : writing => :fast_ruby }
3+ HASH = { writing : :fast_ruby }
44
5- def slow
6- HASH . fetch ( :writing , [ * 1 .. 100 ] )
5+ def fast
6+ HASH . fetch ( :writing ) { "fast ruby" }
77end
88
9- def fast
10- HASH . fetch ( :writing ) { [ * 1 .. 100 ] }
9+ def slow
10+ HASH . fetch ( :writing , "fast ruby" )
1111end
1212
1313Benchmark . ips do |x |
14- x . report ( ' Hash#fetch + arg' ) { slow }
15- x . report ( ' Hash#fetch + block' ) { fast }
14+ x . report ( " Hash#fetch + block" ) { fast }
15+ x . report ( " Hash#fetch + arg" ) { slow }
1616 x . compare!
1717end
You can’t perform that action at this time.
0 commit comments