Skip to content

Instantly share code, notes, and snippets.

@kusor
Created November 22, 2009 15:01
Show Gist options
  • Select an option

  • Save kusor/240591 to your computer and use it in GitHub Desktop.

Select an option

Save kusor/240591 to your computer and use it in GitHub Desktop.
Compare FasterBuilder and Nokogiri::Builder
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'rubygems'
require 'nokogiri'
require 'faster_builder/xml_markup'
require 'benchmark'
def nokogiri_builder
b = Nokogiri::XML::Builder.new do |xml|
xml.task do
xml.progress "10"
xml.success "true"
xml.message "Some String"
xml.updated Time.now.to_s
end
end
b.to_xml
end
def faster_builder
xml = FasterBuilder::XmlMarkup.new
xml.instruct!
xml.task do
xml.tag!("progress", 10)
xml.tag!("success", true)
xml.tag!("message", "Some string")
xml.tag!("updated", Time.now)
end
xml.target!
end
def faster_builder_2
xml = FasterBuilder::XmlMarkup.new
xml.instruct!
xml.task do
xml.progress 10
xml.success true
xml.message "Some string"
xml.updated Time.now
end
xml.target!
end
if __FILE__ == $0
TIMES = 30000
Benchmark.bmbm do |x|
x.report('FasterBuilder') { TIMES.times { faster_builder } }
x.report('FasterBuilder (Method missing)') { TIMES.times { faster_builder_2 } }
x.report('Nokogiri::Builder') { TIMES.times { nokogiri_builder } }
end
end
__END__
$ ruby lib/nokogiri_builder.rb
Rehearsal ------------------------------------------------------------------
FasterBuilder 2.260000 0.020000 2.280000 ( 2.285009)
FasterBuilder (Method missing) 2.480000 0.000000 2.480000 ( 2.495342)
Nokogiri::Builder 3.900000 0.010000 3.910000 ( 3.907372)
--------------------------------------------------------- total: 8.670000sec
user system total real
FasterBuilder 2.180000 0.000000 2.180000 ( 2.177058)
FasterBuilder (Method missing) 2.470000 0.000000 2.470000 ( 2.466327)
Nokogiri::Builder 3.820000 0.010000 3.830000 ( 3.831117)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment