creating large file xml in ruby
Solution 1
If speed is your main concern, I'd just use libxml-ruby directly:
$ time ruby test.rb real 0m7.352suser 0m5.867ssys 0m0.921s
The API is pretty straight forward:
require 'rubygems'require 'xml'doc = XML::Document.new()doc.root = XML::Node.new('root_node')root = doc.root500000.times do |k| root << elem1 = XML::Node.new('products') elem1 << elem2 = XML::Node.new('widget') elem2['id'] = k.to_s elem2['name'] = 'Awesome widget'enddoc.save('foo.xml', :indent => false, :encoding => XML::Encoding::UTF_8)
Using :indent => true
doesn't make much difference in this case, but for more complex XML files it might.
$ time ruby test.rb #(with indent)real 0m7.395suser 0m6.050ssys 0m0.847s
Solution 2
Of course the fastest solution, and that doesn't build up on memory is just to write the XML manually but that will easily generate other sources of error like possibly invalid XML:
$ time ruby test.rb real 0m1.131suser 0m0.873ssys 0m0.126s
Here's the code:
f = File.open("foo.xml", "w")f.puts('<doc>')500000.times do |k| f.puts "<product><widget id=\"#{k}\" name=\"Awesome widget\" /></product>"endf.puts('</doc>')f.close