Sha256: 1b2e4c0c4e3441c9e7e3dfff1ee90b9cde2beae661b9247178fc1840aee32d8c

Contents?: true

Size: 1.01 KB

Versions: 113

Compression:

Stored size: 1.01 KB

Contents

require 'benchmark'
require 'rubygems'
require 'nokogiri'

class Parser < Nokogiri::XML::SAX::Document

  attr_reader :response

  def initialize
    reset
  end

  def reset
    @item = {}
    @response = { :items => [] }
  end

  def characters(string)
    @value ||= ''
    @value << string.strip
  end

  def start_element(name, attrs = [])
    @value = nil
  end

  def end_element(name)
    case name
    when 'item'
      @response[:items] << @item
      @item = {}
    when 'key'
      @item[:key] = value
    end
  end

  def value
    @value.dup
  end

end

data = <<-DATA
<items>
  <item>
    <key>value</key>
  </item>
</items>
DATA

COUNT = 10_000

Benchmark.bmbm(25) do |bench|
  bench.report('parse') do
    COUNT.times do
      parser = Parser.new
      Nokogiri::XML::SAX::Parser.new(parser).parse(data)
      parser.response
    end
  end

  bench.report('push') do
    COUNT.times do
      parser = Parser.new
      Nokogiri::XML::SAX::PushParser.new(parser).write(data, true)
      parser.response
    end
  end
end

Version data entries

113 entries across 113 versions & 18 rubygems

Version Path
fog-1.22.0 benchs/parse_vs_push.rb
fog-1.21.0 benchs/parse_vs_push.rb
fog-maestrodev-1.20.0.20140305101839 benchs/parse_vs_push.rb
fog-maestrodev-1.20.0.20140305101305 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20140212012611 benchs/parse_vs_push.rb
fog-1.20.0 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20140110004459 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20140110003812 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20140109202555 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20140107192102 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20140107142106 benchs/parse_vs_push.rb
fog-maestrodev-1.19.0.20131219203941 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131219193542 benchs/parse_vs_push.rb
fog-1.19.0 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131219033443 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131219032002 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131219030716 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131219022322 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131218202447 benchs/parse_vs_push.rb
fog-maestrodev-1.18.0.20131209091424 benchs/parse_vs_push.rb