Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Forked from biti/haml_vs_erb_vs_erubis.rb
Created October 26, 2011 01:37
Show Gist options
  • Save huacnlee/1315144 to your computer and use it in GitHub Desktop.
Save huacnlee/1315144 to your computer and use it in GitHub Desktop.
erb,erubis,slim,haml的性能比较.结果:haml不靠谱
# encoding: utf-8
require 'rubygems'
require 'haml'
require 'erb'
require 'erubis'
require "slim"
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
slim_templet = <<EOF
doctype html
html
head
title= obj[:title]
body
div id="top"
h1= obj[:title]
div id="main"
p= obj[:description]
ul
- obj[:notes].each do |note|
li= note[:title]
li= note[:content]
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('slim') { Slim::Template.new("/tmp/ssss"){ slim_templet }.render(Object.new, :obj => obj) }
benchmark('erb') { ERB.new(erb_template).result }
benchmark('erubis') { Erubis::Eruby.new(erb_template).result }
haml render time: 34.386436s
slim render time: 24.525808s
erb render time: 3.324529s
erubis render time: 2.793995s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment