Created
November 28, 2012 13:16
-
-
Save bravoecho/4161211 to your computer and use it in GitHub Desktop.
flatten nested hash - recursive vs non-recursive
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
require 'pry' | |
data = { | |
a: { | |
alfa: "alfa", | |
beta: "beta" | |
}, | |
b: 2, | |
c: { | |
alfa: { | |
first: "first", | |
second: "second", | |
third: { | |
x: "x", | |
y: "y", | |
z: { | |
morning: "morning", | |
afternoon: "afternoon", | |
evening: { | |
i: { | |
ii: { | |
iii: { | |
iv: "forth" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
class Flattener | |
def initialize(hash) | |
@hash = hash | |
@result = {} | |
@result_iter = {} | |
@paths = hash.keys.map { |key| [key] } | |
end | |
###################################### | |
# Recursive | |
# | |
def flatten(hash = @hash, old_path = []) | |
hash.each do |key, value| | |
current_path = old_path + [key] | |
if !value.respond_to?(:keys) | |
@result[current_path.join(".")] = value | |
else | |
flatten(value, current_path) | |
end | |
end | |
@result | |
end | |
######################################## | |
# Non recursive | |
# | |
def flatten_iter | |
until @paths.empty? | |
path = @paths.shift | |
value = @hash | |
path.each { |step| value = value[step] } | |
if value.respond_to?(:keys) | |
value.keys.each { |key| @paths << path + [key] } | |
else | |
@result_iter[path.join(".")] = value | |
end | |
end | |
@result_iter | |
end | |
def are_the_same? | |
flatten == flatten_iter | |
end | |
end | |
flattener = Flattener.new(data) | |
require 'pp' | |
pp flattener.flatten | |
pp flattener.flatten_iter | |
pp flattener.are_the_same? | |
require "benchmark" | |
Benchmark.bm do |x| | |
x.report(:recursive) { 100_000.times { flattener.flatten } } | |
x.report(:iterative) { 100_000.times { flattener.flatten_iter } } | |
end | |
Benchmark.bm do |x| | |
x.report(:recursive) { 1_000_000.times { flattener.flatten } } | |
x.report(:iterative) { 1_000_000.times { flattener.flatten_iter } } | |
end | |
# {"a.alfa"=>"alfa", | |
# "a.beta"=>"beta", | |
# "b"=>2, | |
# "c.alfa.first"=>"first", | |
# "c.alfa.second"=>"second", | |
# "c.alfa.third.x"=>"x", | |
# "c.alfa.third.y"=>"y", | |
# "c.alfa.third.z.morning"=>"morning", | |
# "c.alfa.third.z.afternoon"=>"afternoon", | |
# "c.alfa.third.z.evening.i.ii.iii.iv"=>"forth"} | |
# {"b"=>2, | |
# "a.alfa"=>"alfa", | |
# "a.beta"=>"beta", | |
# "c.alfa.first"=>"first", | |
# "c.alfa.second"=>"second", | |
# "c.alfa.third.x"=>"x", | |
# "c.alfa.third.y"=>"y", | |
# "c.alfa.third.z.morning"=>"morning", | |
# "c.alfa.third.z.afternoon"=>"afternoon", | |
# "c.alfa.third.z.evening.i.ii.iii.iv"=>"forth"} | |
# true | |
# user system total real | |
# recursive 4.760000 0.000000 4.760000 ( 4.760669) | |
# iterative 0.010000 0.000000 0.010000 ( 0.011262) | |
# user system total real | |
# recursive 49.080000 0.000000 49.080000 ( 49.070922) | |
# iterative 0.100000 0.000000 0.100000 ( 0.111212) | |
# [Finished in 54.1s] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is some shades of gray here... you have destructive
@paths
manipulations. So everything after run#1 is shows something other. Look, I think this is better:but anyway - thanks! =)