Skip to content

Instantly share code, notes, and snippets.

@nateware
Created January 5, 2010 11:17
Show Gist options
  • Save nateware/269325 to your computer and use it in GitHub Desktop.
Save nateware/269325 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Results (Mac laptop):
# user system total real
# effigy 1.100000 0.010000 1.110000 ( 1.166467)
# erubis 0.260000 0.000000 0.260000 ( 0.265821)
class Post < Struct.new(:id, :title, :body, :comments)
end
class Comment < Struct.new(:id, :title, :summary)
def url
"/comments/#{id}"
end
end
comments = []
comments << Comment.new(1, 'w00t', "First punks!")
comments << Comment.new(2, 'Not sure if this is on', "oinqrvoinq34o[inq o[iq 34n q34[oqoi34090 0 092u43q0 nknvj i ijsie 09lkneproin ]]]")
comments << Comment.new(3, 'Not sure if this is on', "oinqrvoinq34o[inq o[iq 34n q34[oqoi34090 0 092u43q0 nknvj i ijsie 09lkneproin ]]]")
@post = Post.new(1, 'Hello Cleveland!', "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", comments)
$LOAD_PATH.unshift File.dirname(__FILE__)+'/lib'
require 'erubis'
require 'effigy'
require 'benchmark'
template = %{
<html>
<head>
<title></title>
</head>
<body>
<h1></h1>
<p class="body"></p>
<div class="comment">
<h2></h2>
<p></p>
<a>View more</a>
</div>
<p id="no-comments">There aren't any comments for this post.</p>
</body>
</html>
}
class PostView < Effigy::View
attr_reader :post
def initialize(post)
@post = post
end
def transform
text('h1', post.title)
text('title', "#{post.title} - Site title")
text('p.body', post.body)
replace_each('.comment', post.comments) do |comment|
text('h2', comment.title)
text('p', comment.summary)
attr('a', :href => comment.url)
end
remove('#no-comments') if post.comments.empty?
end
end
erb = %{
<html>
<head>
<title><%= @post.title %></title>
</head>
<body>
<h1><%= @post.title %> - Site title</h1>
<p class="body"><%= @post.body %></p>
<% @post.comments.each do |comment| %>
<div class="comment">
<h2><%= comment.title %></h2>
<p><%= comment.summary %></p>
<a href="<%= comment.url %>">View more</a>
</div>
<% end %>
<p id="no-comments">There aren't any comments for this post.</p>
</body>
</html>
}
Benchmark.bm do |bm|
bm.report 'effigy' do
1000.times do
view = PostView.new(@post)
document = view.render(template)
end
end
bm.report 'erubis' do
1000.times do
eruby = Erubis::Eruby.new(erb)
document = eruby.result(binding)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment