Skip to content

Instantly share code, notes, and snippets.

@adamstrickland
Last active March 7, 2019 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamstrickland/a102fe05bc4c747f5ac3010d761cace6 to your computer and use it in GitHub Desktop.
Save adamstrickland/a102fe05bc4c747f5ac3010d761cace6 to your computer and use it in GitHub Desktop.
benchmarking some hash transformation techniques
Rehearsal -------------------------------------------------------
{}.tap 0.000382 0.000257 0.000639 ( 0.000871)
[[]].to_h 0.000031 0.000001 0.000032 ( 0.000032)
{}.each_with_object 0.000027 0.000001 0.000028 ( 0.000028)
---------------------------------------------- total: 0.000699sec
user system total real
{}.tap 0.000030 0.000001 0.000031 ( 0.000029)
[[]].to_h 0.000033 0.000011 0.000044 ( 0.000033)
{}.each_with_object 0.000023 0.000012 0.000035 ( 0.000032)
Rehearsal -------------------------------------------------------
{}.tap 0.000264 0.000166 0.000430 ( 0.000553)
[[]].to_h 0.000023 0.000001 0.000024 ( 0.000023)
{}.each_with_object 0.000019 0.000000 0.000019 ( 0.000020)
---------------------------------------------- total: 0.000473sec
user system total real
{}.tap 0.000023 0.000002 0.000025 ( 0.000024)
[[]].to_h 0.000029 0.000002 0.000031 ( 0.000028)
{}.each_with_object 0.000032 0.000001 0.000033 ( 0.000031)
Rehearsal -------------------------------------------------------
{}.tap 0.000301 0.000152 0.000453 ( 0.000451)
[[]].to_h 0.000029 0.000001 0.000030 ( 0.000029)
{}.each_with_object 0.000031 0.000007 0.000038 ( 0.000037)
---------------------------------------------- total: 0.000521sec
user system total real
{}.tap 0.000040 0.000001 0.000041 ( 0.000039)
[[]].to_h 0.000027 0.000001 0.000028 ( 0.000027)
{}.each_with_object 0.000025 0.000001 0.000026 ( 0.000026)
Rehearsal -------------------------------------------------------
{}.tap 0.000252 0.000146 0.000398 ( 0.000395)
[[]].to_h 0.000021 0.000001 0.000022 ( 0.000020)
{}.each_with_object 0.000023 0.000004 0.000027 ( 0.000026)
---------------------------------------------- total: 0.000447sec
user system total real
{}.tap 0.000027 0.000002 0.000029 ( 0.000026)
[[]].to_h 0.000025 0.000001 0.000026 ( 0.000025)
{}.each_with_object 0.000024 0.000001 0.000025 ( 0.000025)
require "benchmark"
require "rspec"
1.times do
Benchmark.bmbm do |x|
include ::RSpec::Matchers
keys = ("a".."z").to_a.map(&:to_sym)
source_hash = keys.map do |key|
subhash = keys.zip((1..26).to_a).map do |subkey, subval|
[subkey, subval]
end.to_h
[key, subhash]
end.to_h
iterations = 1_000_000
xform = 100
x.report("{}.tap ") do
result = {}.tap do |h|
source_hash.each do |key, subhash|
h[key] = {
a: subhash[:a] * xform,
b: subhash[:b]
}
end
end
expect(result[:a][:a]).to eq xform
expect(result[:b][:a]).to eq xform
expect(result[:a][:b]).to eq 2
expect(result[:b][:b]).to eq 2
end
x.report("[[]].to_h ") do
result = source_hash.map do |key, subhash|
[key, {a: subhash[:a] * xform, b: subhash[:b]}]
end.to_h
expect(result[:a][:a]).to eq xform
expect(result[:b][:a]).to eq xform
expect(result[:a][:b]).to eq 2
expect(result[:b][:b]).to eq 2
end
x.report("{}.each_with_object") do
result = source_hash.each_with_object({}) do |(key, subhash), newhash|
newhash[key] = {a: subhash[:a] * xform, b: subhash[:b]}
end
expect(result[:a][:a]).to eq xform
expect(result[:b][:a]).to eq xform
expect(result[:a][:b]).to eq 2
expect(result[:b][:b]).to eq 2
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment