Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. require 'rubygems'
  2. require 'benchmark'
  3. require 'erb'
  4. require 'erubis'
  5. require 'bundler'
  6.  
  7. #gem 'haml', '3.0.21'
  8. # gem 'haml', '3.1.0.alpha.14'
  9. require 'haml'
  10.  
  11. # 0.6.0 automatically escapes html, using == instead.
  12. gem 'slim', '~> 0.6.1'
  13. require 'slim'
  14.  
  15. erb = <<-ERB
  16. <!doctype html>
  17. <html>
  18. <head>
  19. <title>Test</title>
  20. </head>
  21. <body>
  22. <h1>simple markup</h1>
  23. <div id="content">
  24. <% unless items.empty? %>
  25. <% items.each do |item| %>
  26. <li><%= item %></li>
  27. <% end %>
  28. <% else %>
  29. <p>No items</p>
  30. <% end %>
  31. </div>
  32. <%= tags %>
  33. </body>
  34. </html>
  35. ERB
  36.  
  37. haml= <<-HAML
  38. !!! html
  39. %html
  40. %head
  41. %title Test
  42. %body
  43. %h1 simple markup
  44. #content
  45. - unless items.empty?
  46. - items.each do |item|
  47. %li= item
  48. - else
  49. %p No items
  50. = tags
  51. HAML
  52.  
  53. slim = <<-SLIM
  54. ! doctype html
  55. html
  56. head
  57. title Test
  58. body
  59. h1 simple markup
  60. div#content
  61.  
  62. - unless items.empty?
  63. ol
  64. - items.each do |item|
  65. li
  66. == item
  67. - else
  68. p No items
  69. == tags
  70. SLIM
  71.  
  72. class Context
  73. def items
  74. @items ||= [1, 2, 3, 4, 5]
  75. end
  76.  
  77. def tags
  78. @tags ||= "<p>Something<span>here</span></p>"
  79. end
  80.  
  81. def self.compile_erb(erb)
  82. src = Erubis::Eruby.new(erb).src
  83.  
  84. class_eval <<-RUBY, __FILE__, __LINE__ + 1
  85. def render
  86. #{src}
  87. end
  88. RUBY
  89. end
  90. end
  91.  
  92. cxt = Context.new
  93.  
  94. # Ruby's ERB doesn't like Hash parameters like Erubis
  95. erb_cxt = cxt.instance_eval { binding }
  96.  
  97. n = 10_000
  98.  
  99. e1 = ERB.new(erb)
  100. e2 = Erubis::Eruby.new(erb)
  101. e3 = Erubis::FastEruby.new(erb)
  102. e4 = Context.compile_erb(erb)
  103. h1 = Haml::Engine.new(haml, :format => :html5)
  104. h2 = Haml::Engine.new(haml, :format => :html5, :ugly => true)
  105. s1 = Slim::Engine.new(slim)
  106.  
  107. ec = Rubinius::Compiler.eval_cache
  108.  
  109. h = { :items => cxt.items, :tags => cxt.tags }
  110.  
  111. Benchmark.bmbm do |bench|
  112. # bench.report('emtpy loop') { n.times { } }
  113.  
  114. bench.report('erb') { n.times { e1.result(erb_cxt) } }
  115. bench.report("erubis #{Erubis::VERSION} (hash)") {
  116. n.times { e2.result(h) }
  117. }
  118. bench.report("erubis #{Erubis::VERSION} (binding)") {
  119. n.times { e2.result(erb_cxt) }
  120. }
  121. bench.report("fast erubis #{Erubis::VERSION} (hash)") {
  122. n.times { e3.result(:items => cxt.items, :tags => cxt.tags) }
  123. }
  124. bench.report("fast erubis #{Erubis::VERSION} (binding)") {
  125. n.times { e3.result(erb_cxt) }
  126. }
  127. bench.report("erubis #{Erubis::VERSION} (compiled)") {
  128. n.times { cxt.render }
  129. }
  130.  
  131. bench.report("haml #{Haml::VERSION}") {
  132. n.times { h1.render(cxt) }
  133. }
  134. bench.report("haml (ugly) #{Haml::VERSION}") {
  135. n.times { h2.render(cxt) }
  136. }
  137. bench.report('slim') { n.times { s1.render(cxt) } }
  138. end
  139.  
  140. p ec.current
  141. p :fin => ec.misses
Add Comment
Please, Sign In to add comment