Skip to content

Commit d373fb6

Browse files
committed
Use a common example for hash fetch.
Closes #35.
1 parent dbbf7f4 commit d373fb6

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff 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
389389
Calculating -------------------------------------
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.562k5.2%) i/s - 923.350k
394-
Hash#fetch + block 5.880M7.5%) i/s - 29.311M
393+
Hash#fetch + block 6.116M8.9%) i/s - 30.354M
394+
Hash#fetch + arg 4.473M9.9%) i/s - 22.134M
395395
396396
Comparison:
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)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
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" }
77
end
88

9-
def fast
10-
HASH.fetch(:writing) { [*1..100] }
9+
def slow
10+
HASH.fetch(:writing, "fast ruby")
1111
end
1212

1313
Benchmark.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!
1717
end

0 commit comments

Comments
 (0)