-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change: add another slicing method, and add a note about assuming all…
… keys being in the Hash.
- Loading branch information
Showing
3 changed files
with
44 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'benchmark/ips' | ||
|
||
HASH = { | ||
a: 'foo', | ||
b: 'bar', | ||
c: 'baz', | ||
d: 'qux' | ||
}.freeze | ||
|
||
# Only keys that exist in the hash. | ||
KEYS = %i[a c].freeze | ||
|
||
def fast | ||
HASH.values_at(*KEYS) | ||
end | ||
|
||
def slow | ||
KEYS.map { |key| HASH[key] } | ||
end | ||
|
||
def slowest | ||
HASH.slice(*KEYS).values | ||
end | ||
|
||
Benchmark.ips do |x| | ||
x.report('Hash#values_at ') { fast } | ||
x.report('Array#map { Hash#[] }') { slow } | ||
x.report('Hash#slice#values ') { slowest } | ||
x.compare! | ||
end |
This file was deleted.
Oops, something went wrong.