#!/usr/bin/env ruby require 'rubygems' require 'benchmark' require 'hpricot' require 'rexml/document' require 'xml' # Taken from http://depixelate.com/2008/4/23/ruby-xml-parsing-benchmarks XML_STRING = DATA.read class Parse def self.libxml doc = XML::Parser.string(XML_STRING).parse ary = [] doc.find('/*/*/*').each do |node| case node.name when 'ItemQueryRs' node.each_element do |child| ary << child.find_first('./ListID') end end end ary end def self.rexml doc = REXML::Document.new(XML_STRING) ary = [] REXML::XPath.each(doc, '/*/*/*') do |node| case node.name when 'ItemQueryRs' node.elements.each do |element| ary << rexml_fetch(element, 'ListID') end end end ary end def self.hpricot doc = Hpricot.XML(XML_STRING) ary = [] response_element = doc.search('/*/*/*').each do |node| next unless node.elem? case node.name when 'ItemQueryRs' node.containers.each do |element| ary << hpricot_fetch(element/'ListID') end end end ary end # rexml helper def self.rexml_fetch(node, name) e = REXML::XPath.first(node, name) e ? e.text : nil end # hpricot helper def self.hpricot_fetch(path) return nil if path.nil? || path.empty? path = path.first if path.is_a?(Array) path.innerHTML end end TIMES = 10 Benchmark.bmbm do |x| x.report('libxml') { TIMES.times { Parse.libxml } } x.report('Hpricot') { TIMES.times { Parse.hpricot } } x.report('REXML') { TIMES.times { Parse.rexml } } end __END__ 240000-1071531214 2003-12-15T15:33:34-08:00 2003-12-15T15:34:51-08:00 1071531291 Delivery Delivery true 0 20000-999021789 Non Delivery Service Fee (free for orders over $100) 15.00 610001-1071531179 Service 10000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Design Design true 0 20000-999021789 Non Custom Landscape Design 55.00 150000-934380913 Landscaping Services:Design Services 20000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T08:59:12-08:00 934387152 Gardening Gardening true 0 20000-999021789 Non Weekly gardening services 0.00 1F0000-934380913 Landscaping Services:Labor:Installation 30000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Installation Installation true 0 20000-999021789 Non Installation of landscape design 35.00 1F0000-934380913 Landscaping Services:Labor:Installation 40000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Pest Control Pest Control true 0 20000-999021789 Non Pest control services 0.00 200000-934380913 Landscaping Services:Labor:Maintenance & Repairs 2E0000-1071514896 2003-12-15T11:01:36-08:00 2003-12-15T14:42:51-08:00 1071528171 Tree Removal Tree Removal true 0 20000-999021789 Non Tree Removal Service 0.00 610001-1071531179 Service 50000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Trimming Trimming true 0 20000-999021789 Non Tree and shrub trimming 35.00 200000-934380913 Landscaping Services:Labor:Maintenance & Repairs B0000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Concrete Concrete true 0 10000-999021789 Tax Concrete for fountain installation 0.00 1B0000-934380913 Landscaping Services:Job Materials:Fountains & Garden Lighting C0000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Deck Lumber Deck Lumber true 0 10000-999021789 Tax Deck Lumber 0.00 1A0000-934380913 Landscaping Services:Job Materials:Decks & Patios 210000-1071530240 2003-12-15T15:17:20-08:00 2003-12-15T15:17:20-08:00 1071530240 Fertilizer Fertilizer true 0 10000-999021789 Tax Parent Item - Do Not Use 0.00 600001-1071530232 Retail Sales 250000-1071523682 2003-12-15T13:28:02-08:00 2003-12-15T13:44:20-08:00 1071524660 Irrigation Hose Irrigation Hose true 0 10000-999021789 Tax Parent Item Vinyl Irrigation Line- Do Not Purchase or Sell 0.00 690001-1071523679 Landscaping Services:Job Materials:Misc Materials Vinyl Irrigation LineParent Item - Do Not Purchase or Sell 0.00 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset -1 0 0.00 0 0 270000-1071524193 2003-12-15T13:36:33-08:00 2003-12-15T13:38:13-08:00 1071524293 1/2" Line Irrigation Hose:1/2" Line true 250000-1071523682 Irrigation Hose 1 10000-999021789 Tax 1/2" Vinyl Irrigation Line 0.15 690001-1071523679 Landscaping Services:Job Materials:Misc Materials 1/2" Vinyl Irrigation Line 0.12 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 1500 1783 0.12 0 0 260000-1071523858 2003-12-15T13:30:58-08:00 2003-12-15T13:37:52-08:00 1071524272 1/4" Line Irrigation Hose:1/4" Line true 250000-1071523682 Irrigation Hose 1 10000-999021789 Tax 1/4" Vinyl Irrigation Line 0.10 690001-1071523679 Landscaping Services:Job Materials:Misc Materials 1/4" Vinyl Irrigation Line 0.07 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 500 1235 0.07 0 0 280000-1071524260 2003-12-15T13:37:40-08:00 2003-12-15T13:37:40-08:00 1071524260 3/4" Line Irrigation Hose:3/4" Line true 250000-1071523682 Irrigation Hose 1 10000-999021789 Tax 3/4" Vinyl Irrigation Line 0.27 690001-1071523679 Landscaping Services:Job Materials:Misc Materials 3/4" Vinyl Irrigation Line 0.18 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 1500 2670 0.18 0 0 60000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Lighting Lighting true 0 10000-999021789 Tax Garden Lighting 0.00 1B0000-934380913 Landscaping Services:Job Materials:Fountains & Garden Lighting Garden Lighting 0.00 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 94 14.80 28 0 70000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Pump Pump true 0 10000-999021789 Tax Fountain pump 75.00 1B0000-934380913 Landscaping Services:Job Materials:Fountains & Garden Lighting Fountain pump #198-30 56.00 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 48 53.93 0 0 80000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Soil Soil true 0 10000-999021789 Tax Soil, 2 cubic ft bag 6.75 1C0000-934380913 Landscaping Services:Job Materials:Plants and Sod Soil, 2 cubic ft bag 5.30 240000-934380913 Cost of Goods Sold 10000-934380927 Middlefield Nursery 60000-934380912 Inventory Asset 25 0 5.30 6 10 90000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Sprinkler Hds Sprinkler Hds true 0 10000-999021789 Tax Sprinkler heads 0.00 1D0000-934380913 Landscaping Services:Job Materials:Sprinklers & Drip systems Sprinkler head #BLS9081-09 0.00 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 69 6.38 36 0 A0000-934380927 1999-08-11T07:15:27-08:00 1999-08-11T07:15:27-08:00 934380927 Sprkl pipes Sprkl pipes true 0 10000-999021789 Tax Plastic sprinkler piping 2.75 1D0000-934380913 Landscaping Services:Job Materials:Sprinklers & Drip systems Plastic sprinkler piping #1098-20 2.10 240000-934380913 Cost of Goods Sold 60000-934380912 Inventory Asset 250 826 2.10 115 0