jamie (owner)

Revisions

gist: 65299 Download_button fork
public
Public Clone URL: git://gist.github.com/65299.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# # 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