Skip to content

Commit

Permalink
Added: Hash#values_at vs Hash#slice#values.
Browse files Browse the repository at this point in the history
  • Loading branch information
kewlar committed Mar 14, 2019
1 parent 9ac23c8 commit edb2335
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,26 @@ Array#each_w/_object: 1352851.8 i/s - 1.88x slower
Hash#select-include : 760944.2 i/s - 3.34x slower
```

##### `Hash#values_at` vs `Hash#slice#values`
[code](code/hash/values_at-vs-slice-values.rb)

To select hash values by keys.

```
$ ruby -v code/hash/values_at-vs-slice-values.rb
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
Warming up --------------------------------------
Hash#values_at 284.628k i/100ms
Hash#slice#values 230.279k i/100ms
Calculating -------------------------------------
Hash#values_at 7.179M (± 7.4%) i/s - 35.863M in 5.034179s
Hash#slice#values 4.458M (± 4.1%) i/s - 22.337M in 5.019922s
Comparison:
Hash#values_at : 7178610.6 i/s
Hash#slice#values: 4458017.0 i/s - 1.61x slower
```


### Proc & Block

Expand Down
23 changes: 23 additions & 0 deletions code/hash/values_at-vs-slice-values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'benchmark/ips'

HASH = {
one: 'foo',
two: 'bar',
three: 'baz',
four: 'qux'
}
KEYS = %i[one three]

def fast
HASH.values_at(*KEYS)
end

def slow
HASH.slice(*KEYS).values
end

Benchmark.ips do |x|
x.report('Hash#values_at ') { fast }
x.report('Hash#slice#values') { slow }
x.compare!
end

0 comments on commit edb2335

Please sign in to comment.