Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'benchmark'
- require 'erb'
- require 'erubis'
- require 'bundler'
- #gem 'haml', '3.0.21'
- # gem 'haml', '3.1.0.alpha.14'
- require 'haml'
- # 0.6.0 automatically escapes html, using == instead.
- gem 'slim', '~> 0.6.1'
- require 'slim'
- erb = <<-ERB
- <!doctype html>
- <html>
- <head>
- <title>Test</title>
- </head>
- <body>
- <h1>simple markup</h1>
- <div id="content">
- <% unless items.empty? %>
- <% items.each do |item| %>
- <li><%= item %></li>
- <% end %>
- <% else %>
- <p>No items</p>
- <% end %>
- </div>
- <%= tags %>
- </body>
- </html>
- ERB
- haml= <<-HAML
- !!! html
- %html
- %head
- %title Test
- %body
- %h1 simple markup
- #content
- - unless items.empty?
- - items.each do |item|
- %li= item
- - else
- %p No items
- = tags
- HAML
- slim = <<-SLIM
- ! doctype html
- html
- head
- title Test
- body
- h1 simple markup
- div#content
- - unless items.empty?
- ol
- - items.each do |item|
- li
- == item
- - else
- p No items
- == tags
- SLIM
- class Context
- def items
- @items ||= [1, 2, 3, 4, 5]
- end
- def tags
- @tags ||= "<p>Something<span>here</span></p>"
- end
- def self.compile_erb(erb)
- src = Erubis::Eruby.new(erb).src
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
- def render
- #{src}
- end
- RUBY
- end
- end
- cxt = Context.new
- # Ruby's ERB doesn't like Hash parameters like Erubis
- erb_cxt = cxt.instance_eval { binding }
- n = 10_000
- e1 = ERB.new(erb)
- e2 = Erubis::Eruby.new(erb)
- e3 = Erubis::FastEruby.new(erb)
- e4 = Context.compile_erb(erb)
- h1 = Haml::Engine.new(haml, :format => :html5)
- h2 = Haml::Engine.new(haml, :format => :html5, :ugly => true)
- s1 = Slim::Engine.new(slim)
- ec = Rubinius::Compiler.eval_cache
- h = { :items => cxt.items, :tags => cxt.tags }
- Benchmark.bmbm do |bench|
- # bench.report('emtpy loop') { n.times { } }
- bench.report('erb') { n.times { e1.result(erb_cxt) } }
- bench.report("erubis #{Erubis::VERSION} (hash)") {
- n.times { e2.result(h) }
- }
- bench.report("erubis #{Erubis::VERSION} (binding)") {
- n.times { e2.result(erb_cxt) }
- }
- bench.report("fast erubis #{Erubis::VERSION} (hash)") {
- n.times { e3.result(:items => cxt.items, :tags => cxt.tags) }
- }
- bench.report("fast erubis #{Erubis::VERSION} (binding)") {
- n.times { e3.result(erb_cxt) }
- }
- bench.report("erubis #{Erubis::VERSION} (compiled)") {
- n.times { cxt.render }
- }
- bench.report("haml #{Haml::VERSION}") {
- n.times { h1.render(cxt) }
- }
- bench.report("haml (ugly) #{Haml::VERSION}") {
- n.times { h2.render(cxt) }
- }
- bench.report('slim') { n.times { s1.render(cxt) } }
- end
- p ec.current
- p :fin => ec.misses
Add Comment
Please, Sign In to add comment