Is creating a Nokogiri::XML::Builder object an expensive process?
I have a list of about 1,000,000 ActiveRecord objec开发者_Python百科ts that I am using Nokogiri to build. Right now I am calling a method on each object to create a builder and return it. THen I write that builder to an already open file with:
builder.to_xml
It works fine, but I was wondering if somehow I could create an empty builder element and clone it (or something, that might be just as expensive of an operation) and send it to the AR instance method.
For 1,000,000 objects I'd look into using ERB or Erubis to generate the XML using a hand-coded template.
Nokogiri is dynamically creating each element, which is costly in comparison to the speed of a process that already knows what to do for each object.
Erubis is faster than ERB, but ERB is the standard for Rails. The templates are interchangeable.
Another alternate is to see if you can get your database to output the selected records as XML based on a query. It's going to be the fastest way to generate the data file.
精彩评论