Skip to content

Instantly share code, notes, and snippets.

@biti
Created June 25, 2011 10:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save biti/1046340 to your computer and use it in GitHub Desktop.
Save biti/1046340 to your computer and use it in GitHub Desktop.
erb,erubis,haml的性能比较.结果:haml不靠谱
# encoding: utf-8
require 'rubygems'
require 'haml'
require 'erb'
require 'erubis'
notes = []
20.times { notes << {:title => "标题标题", :content => "内容,内容"} }
obj = {
:title => '笔记本首页',
:description => '笔记本' * 100,
:notes => notes
}
haml_template = <<EOF
!!!
%html
%head
%title= obj[:title]
%body
#top
%h1= obj[:title]
#main
%p= obj[:description]
%ul
- obj[:notes].each do |note|
%li= note[:title]
%li= note[:content]
#foot
copy right
EOF
erb_template = <<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title><%= obj[:title] %></title></head>
<body>
<div id='top'>
<h1><%= obj[:title] %></h1>
</div>
<div id='main'>
<p><%= obj[:description] %></p>
<ul>
<% obj[:notes].each do |n| %>
<li><%= n[:title] %></li>
<li><%= n[:content] %></li><% end %>
</ul>
<div id='foot'>
copy right
</div>
</body>
</html>
EOF
def benchmark(engine)
start = Time.now
10000.times do
yield
end
seconds = Time.now - start
puts "%s render time: %ss" % [engine, seconds]
end
benchmark('haml') { Haml::Engine.new(haml_template).render(Object.new, :obj => obj) }
benchmark('erb') { ERB.new(erb_template).result }
benchmark('erubis') { Erubis::Eruby.new(erb_template).result }
# haml render time: 44.158047267s
# erb render time: 4.431236469s
# erubis render time: 3.513889438s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment