Skip to content

Instantly share code, notes, and snippets.

@Serabe
Created November 30, 2009 21:54
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 Serabe/245784 to your computer and use it in GitHub Desktop.
Save Serabe/245784 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
require 'benchmark'
puts Nokogiri.uses_libxml?
orig_doc = Nokogiri::XML::Document.new
root = Nokogiri::XML::Node.new "employees", orig_doc
orig_doc.root = root
["Margareth", "John", "Aaron", "Mike", "Sergio"].each do |name|
[22, 25, 28, 31, 34].each do |age|
["programmer", "manager", "nothing, actually"].each do |job|
employee_node = Nokogiri::XML::Node.new "employee", orig_doc
name_node = Nokogiri::XML::Node.new "name", orig_doc
name_node.content = name
age_node = Nokogiri::XML::Node.new "age", orig_doc
age_node.content = age
job_node = Nokogiri::XML::Node.new "job", orig_doc
job_node.content = job
employee_node << name_node
employee_node << age_node
employee_node << job_node
root << employee_node
end
end
end
doc1 = orig_doc.dup(1)
doc2 = orig_doc.dup(1)
office = Nokogiri::XML::Node.new "office", doc1
state = Nokogiri::XML::Node.new "state", doc1
state.content = "Some state"
office << state
other_data = Nokogiri::XML::Node.new "other_data", doc2
frag1 = Nokogiri::XML::DocumentFragment.new doc1
frag1 << office
frag1 << other_data
office = Nokogiri::XML::Node.new "office", doc2
state = Nokogiri::XML::Node.new "state", doc2
state.content = "Some state"
office << state
other_data = Nokogiri::XML::Node.new "other_data", doc2
Benchmark.bm do |b|
b.report "with fragment" do
500.times do
doc = doc1.dup(1)
doc.search('//employee').each do |node|
node << frag1.dup(1)
end
end
end
b.report "without fragment" do
500.times do
doc = doc1.dup(1)
doc.search('//employee').each do |node|
node << office.dup(1)
node << other_data.dup(1)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment