Skip to content

Instantly share code, notes, and snippets.

@jamie
Created February 16, 2009 18:52
Show Gist options
  • Save jamie/65299 to your computer and use it in GitHub Desktop.
Save jamie/65299 to your computer and use it in GitHub Desktop.
# # of iterations = 10000
# Rehearsal -------------------------------------------------------
# null_time 0.000000 0.000000 0.000000 ( 0.002980)
# erubis 0.680000 0.010000 0.690000 ( 0.689765)
# builder 1.040000 0.010000 1.050000 ( 1.055823)
# erector 1.360000 0.000000 1.360000 ( 1.373215)
# tagz 2.420000 0.020000 2.440000 ( 2.453186)
# haml 7.090000 0.030000 7.120000 ( 7.196357)
# markaby 9.680000 0.040000 9.720000 ( 9.824390)
# --------------------------------------------- total: 22.380000sec
#
# user system total real
# null_time 0.010000 0.000000 0.010000 ( 0.003117)
# erubis 0.650000 0.010000 0.660000 ( 0.663166)
# builder 1.040000 0.000000 1.040000 ( 1.052085)
# erector 1.310000 0.010000 1.320000 ( 1.329529)
# tagz 2.420000 0.010000 2.430000 ( 2.454841)
# haml 7.050000 0.030000 7.080000 ( 7.109425)
# markaby 9.620000 0.040000 9.660000 ( 9.723050)
#!/usr/bin/env ruby
require 'benchmark'
require 'rubygems'
require 'markaby'
require 'tagz'
require 'haml'
require 'erubis'
require 'erector'
def do_builder
Builder::XmlMarkup.new.html { |xm|
xm.head {
xm.title "happy title"
}
xm.body {
xm.h1 "happy heading"
xm.a "a link", :href => "url"
}
}
end
class ErectorPage < Erector::Widget
def render
html do
head do
title "happy title"
end
body do
h1 "happy heading"
a "a link", :href => "url"
end
end
end
end
def do_erector
ErectorPage.new.to_s
end
def do_erubis
str = <<-EOF
<html>
<head>
<title>happy title</title>
</head>
<body>
<h1>happy heading</h1>
<a href="<%= 'url' %>">a link</a>
</body>
</html>
EOF
Erubis::Eruby.new(str).result
end
def do_haml
str = <<-EOF
%html
%head
%title happy title
%body
%h1 happy heading
%a{:href => "url"} a link
EOF
Haml::Engine.new(str).to_html
end
def do_markaby
mab = Markaby::Builder.new :output_meta_tag => false
mab.html {
head {
title "happy title"
}
body {
h1 "happy heading"
a "a link", :href => "url"
}
}.to_s
end
def do_tagz
Tagz {
html_ {
head_ {
title_ "happy title"
}
body_ {
h1_ "happy heading"
a_ "a link", :href => "url"
}
}
}
end
baseline = do_tagz
out = do_markaby
raise "bad markaby!\n\n#{baseline}\n\n#{out}\n" unless baseline == out
out = do_builder
raise "bad builder!\n\n#{baseline}\n\n#{out}\n" unless baseline == out
out = do_erubis.gsub(/\n */, '')
raise "bad erubis!\n\n#{baseline}\n\n#{out}\n" unless baseline == out
out = do_haml.gsub(/\n */, '').gsub("'", '"')
raise "bad haml!\n\n#{baseline}\n\n#{out}\n" unless baseline == out
out = do_erector
raise "bad erector!\n\n#{baseline}\n\n#{out}\n" unless baseline == out
def do_nothing
# do nothing
end
max = (ARGV.shift || 1_000_000).to_i
puts "# of iterations = #{max}"
Benchmark::bmbm(20) do |x|
x.report("null_time") do
for i in 0..max do
do_nothing
end
end
x.report("erubis") do
for i in 0..max do
do_erubis
end
end
x.report("builder") do
for i in 0..max do
do_builder
end
end
x.report("erector") do
for i in 0..max do
do_erector
end
end
x.report("tagz") do
for i in 0..max do
do_tagz
end
end
x.report("haml") do
for i in 0..max do
do_haml
end
end
x.report("markaby") do
for i in 0..max do
do_markaby
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment