Skip to content

Instantly share code, notes, and snippets.

@siannopollo
Forked from chanks/benchmark.rb
Last active February 25, 2023 07:01
Show Gist options
  • Save siannopollo/1807036 to your computer and use it in GitHub Desktop.
Save siannopollo/1807036 to your computer and use it in GitHub Desktop.
Benchmark haml vs. erb
require 'rubygems'
require 'benchmark'
require 'haml'
haml_template = File.read('template.haml')
erb_template = File.read('template.erb')
Benchmark.bm do |x|
x.report('haml:') { 1000.times{ Haml::Engine.new(haml_template).render } }
x.report('erb:') { 1000.times{ ERB.new(erb_template, nil, '-').src } }
end
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-US' lang='en-US'>
<head>
<title>Hampton Catlin Is Totally Awesome</title>
<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
</head>
<body>
<!-- You're In my house now! -->
<div class='header'>
Yes, ladies and gentileman. He is just that egotistical.
Fantastic! This should be multi-line output
The question is if this would translate! Ahah!
<%= 1 + 9 + 8 + 2 %>
<%# numbers should work and this should be ignored %>
</div>
<% 120.times do |number| -%>
<%= number %>
<% end -%>
<div id='body'><%= " Quotes should be loved! Just like people!" %></div>
Wow.
<p code="<%= 1 + 2 %>">
<%= "Holy cow " +
"multiline " +
"tags! " +
"A pipe (|) even!" %>
<%= [1, 2, 3].collect { |n| "PipesIgnored|" }.join %>
<%= [1, 2, 3].collect { |n|
n.to_s
}.join("|") %>
</p>
<% bar = 17 %>
<div class='silent' foo="<%= bar %>">
<% foo = String.new
foo << "this"
foo << " shouldn't"
foo << " evaluate" %>
<%= foo + "but now it should!" %>
<%# Woah crap a comment! %>
</div>
<ul class='really cool'>
<% ('a'..'f').each do |a|%>
<li><%= a %></li>
<% end %>
<div class='of_divs_with_underscore' id='combo'><%= @should_eval = "with this text" %></div>
<%= "foo".each_line do |line|
nil
end %>
<div class='footer'>
<strong class='shout'>
<%= "This is a really long ruby quote. It should be loved and wrapped because its more than 50 characters. This value may change in the future and this test may look stupid.\n" +
" So, I'm just making it *really* long. God, I hope this works" %>
</strong>
</div>
</body>
</html>
!!!
%html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en-US", "lang" => "en-US"}
%head
%title Hampton Catlin Is Totally Awesome
%meta{"http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"}
%body
/ You're In my house now!
.header
Yes, ladies and gentileman. He is just that egotistical.
Fantastic! This should be multi-line output
The question is if this would translate! Ahah!
= 1 + 9 + 8 + 2 #numbers should work and this should be ignored
#body= " Quotes should be loved! Just like people!"
- 120.times do |number|
= number
Wow.|
%p{:code => 1 + 2}
= "Holy cow " + |
"multiline " + |
"tags! " + |
"A pipe (|) even!" |
= [1, 2, 3].collect { |n| "PipesIgnored|" }.join
= [1, 2, 3].collect { |n| |
n.to_s |
}.join("|") |
- bar = 17
%div.silent{:foo => bar}
- foo = String.new
- foo << "this"
- foo << " shouldn't"
- foo << " evaluate"
= foo + " but now it should!"
-# Woah crap a comment!
-# That was a line that shouldn't close everything.
%ul.really.cool
- ('a'..'f').each do |a|
%li= a
#combo.of_divs_with_underscore= @should_eval = "with this text"
= "foo".each_line do |line|
- nil
.footer
%strong.shout= "This is a really long ruby quote. It should be loved and wrapped because its more than 50 characters. This value may change in the future and this test may look stupid. \nSo, I'm just making it *really* long. God, I hope this works"
@siannopollo
Copy link
Author

Created to answer this Stack Overflow question.

@spickermann
Copy link

spickermann commented May 15, 2020

I wonder why the benchmark works differently for haml and ERB? The haml version initializes the haml engine just once outside of the benchmark. But the ERB version initializes a new engine each time in the benchmark.

I change the benchmark to initialize the engines for both in the benchmark like this:

haml_template = File.read('template.haml')
erb_template = File.read('template.erb')

Benchmark.bm do |x|
  x.report('haml:') { 1000.times{ Haml::Engine.new(haml_template).render } }
  x.report('erb:') { 1000.times{ ERB.new(erb_template, nil, '-').src } }
end

And with this change, haml is not only a bit slower than ERB. It needs about 20 times as long as ERB:

$ ruby benchmark.rb 
       user       system     total        real
haml:  5.914779   0.048249   5.963028 (  6.104936)
erb:   0.297913   0.001730   0.299643 (  0.303349)

@siannopollo
Copy link
Author

@spickermann Thanks for pointing that out. I know this gist and the accompanying SO question are pretty old, but having bad info out there is no good for anyone. I updated the gist and the SO question with the corrected code and updated benchmarks from my machine.

$ ruby benchmark.rb 
       user     system      total        real
haml:  4.030000   0.020000   4.050000 (  4.070071)
erb:  0.270000   0.000000   0.270000 (  0.268536)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment