Skip to content

Instantly share code, notes, and snippets.

@aishfenton
Created July 18, 2010 01:08
Show Gist options
  • Save aishfenton/480004 to your computer and use it in GitHub Desktop.
Save aishfenton/480004 to your computer and use it in GitHub Desktop.
require 'rubygems'
require "builder"
require "benchmark"
require 'nokogiri'
require 'erb'
require 'erubis'
ITERATIONS = 1_000
ERB_TEMPLATE = <<-EOL
<jobs>
<%
ITERATIONS.times do
%>
<job>
<zid><%= 123 %></zid>
<name><%= "job" + rand(100).to_s %></name>
<state><%= "Started" %></state>
<resource_id><%= 1 %></resource_id>
<steps>
<%
20.times do
%>
<zid><%= 123 %></zid>
<name><%= "Milestone" + rand(1000).to_s %></name>
<completed_at><%= Time.now.to_s %></completed_at>
<%
end
%>
</steps>
</job>
<%
end
%>
<jobs>
EOL
def do_nokogiri
xml = Nokogiri::XML::Builder.new
build_xml(xml)
end
def do_builder
xml = Builder::XmlMarkup.new
build_xml(xml)
end
def do_erubis
template = Erubis::Eruby.new(ERB_TEMPLATE)
template.result(binding())
end
def do_erb
template = ERB.new(ERB_TEMPLATE)
template.result(binding)
end
def build_xml(xml)
xml.jobs do
ITERATIONS.times do
xml.job do
xml.zid "123"
xml.name "My REALLY BIG Job"
xml.state "Started"
xml.resource_id "1"
xml.steps do
20.times do
xml.step do
xml.zid "123"
xml.name "Milestone"
xml.completed_at Time.now.to_s
end
end
end
end
end
end
end
Benchmark.bmbm do |x|
x.report("Erb") { do_erb }
x.report("Erubis") { do_erubis }
x.report("Nokogiri") { do_nokogiri }
x.report("Builder") { do_builder }
end
# user system total real
# Erb 0.160000 0.000000 0.160000 ( 0.168597)
# Erubis 0.180000 0.010000 0.190000 ( 0.182917)
# Nokogiri 3.510000 0.030000 3.540000 ( 3.548115)
# Builder 3.310000 0.050000 3.360000 ( 3.362523)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment