-
-
Save huacnlee/1315144 to your computer and use it in GitHub Desktop.
erb,erubis,slim,haml的性能比较.结果:haml不靠谱
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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