Skip to content

Instantly share code, notes, and snippets.

@dorkalev
Created April 20, 2010 21:13
Show Gist options
  • Save dorkalev/373080 to your computer and use it in GitHub Desktop.
Save dorkalev/373080 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :children
attr_accessor :title
def general_deep_copy
Marshal.load(Marshal.dump(self))
end
def specific_deep_copy
Node.new(@title,children.map(&:specific_deep_copy))
end
def initialize(title_,children_ = [])
@title = title_
@children = children_
end
def inspect
if children.empty?
title.is_a?(String) ? title : "<#{title}></#{title}>"
else
"<#{title}>#{children.map(&:inspect).join("")}</#{title}>"
end
end
end
def prof(t = 1)
starting_time = Time.now
t.times { yield }
dlta = (Time.now-starting_time).to_f/t
puts "time: #{dlta.to_s}(s) . req/s: #{(1/dlta)}"
"time: #{dlta.to_s}(s) . req/s: #{(1/dlta)}"
end
root = Node.new(:html,[Node.new(:head),Node.new(:body,[Node.new(:div,[Node.new(:inner_html,[Node.new('hello world')])])])])
puts 'general deep copy:'
prof(100000) {
root.general_deep_copy
}
puts root.general_deep_copy.inspect
puts
puts
puts 'specific deep copy:'
prof(100000) {
root.specific_deep_copy
}
puts root.specific_deep_copy.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment