File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -301,6 +301,31 @@ Comparison:
301301
302302### Hash
303303
304+ ##### ` Hash#[] ` vs ` Hash#dup ` [ code] ( code/hash/bracket-vs-dup.rb )
305+
306+ Source: http://tenderlovemaking.com/2015/02/11/weird-stuff-with-hashes.html
307+
308+ > Does this mean that you should switch to Hash[ ] ?
309+ > Only if your benchmarks can prove that it’s a bottleneck.
310+ > Please please please don’t change all of your code because
311+ > this shows it’s faster. Make sure to measure your app performance first.
312+
313+ ```
314+ $ ruby -v code/hash/bracket-vs-dup.rb
315+ ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
316+
317+ Calculating -------------------------------------
318+ Hash[] 29.403k i/100ms
319+ Hash#dup 16.195k i/100ms
320+ -------------------------------------------------
321+ Hash[] 343.987k (± 8.7%) i/s - 1.735M
322+ Hash#dup 163.516k (±10.2%) i/s - 825.945k
323+
324+ Comparison:
325+ Hash[]: 343986.5 i/s
326+ Hash#dup: 163516.3 i/s - 2.10x slower
327+ ```
328+
304329##### ` Hash#fetch ` with argument vs ` Hash#fetch ` + block [ code] ( code/hash/fetch-vs-fetch-with-block.rb )
305330
306331```
Original file line number Diff line number Diff line change 1+ require 'benchmark/ips'
2+
3+ HASH = Hash [ *( 'a' ..'z' ) . to_a ]
4+
5+ def slow
6+ HASH . dup
7+ end
8+
9+ def fast
10+ Hash [ HASH ]
11+ end
12+
13+ Benchmark . ips do |x |
14+ x . report ( "Hash[]" ) { fast }
15+ x . report ( "Hash#dup" ) { slow }
16+ x . compare!
17+ end
You can’t perform that action at this time.
0 commit comments