I want to write approximately 50MB of data to an XML file.
I found Nokogiri (1.5.0) to be efficient for parsing when just reading and not writing. Nokogiri is not a good option to write to an XML file since it holds the complete XML data in memory until it finally writes it.
I found Builder (3.0.0) to be a good option but I'm not sure if it's the best option.
I tried some benchmarks with the following simple code:
(1..500000).each do |k|
xml.products {
xml.widget {
xml.id_ k
xml.name "Awesome widget"
}
}
end
Nokogiri takes about 143 seconds and also memory consumption gradually increased and ended at about 700 MB.
Builder took about 123 seconds and memory consumption was stable enough at 10 MB.
So is there a better solution to write huge XML files (50 MB) in Ruby?
Here's the code using Nokogiri:
require 'rubygems'
require 'nokogiri'
a = Time.now
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
(1..500000).each do |k|
xml.products {
xml.widget {
xml.id_ k
xml.name "Awesome widget"
}
}
end
}
end
o = File.new("test_noko.xml", "w")
o.write(builder.to_xml)
o.close
puts (Time.now-a).to_s
Here's the code using Builder:
require 'rubygems'
require 'builder'
a = Time.now
File.open("test.xml", 'w') {|f|
xml = Builder::XmlMarkup.new(:target => f, :indent => 1)
(1..500000).each do |k|
xml.products {
xml.widget {
xml.id_ k
xml.name "Awesome widget"
}
}
end
}
puts (Time.now-a).to_s