1
0
Fork 0

Initial commit

This commit is contained in:
Gregory Eremin 2013-12-13 13:39:20 +07:00
commit 1af11cdea2
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
7 changed files with 144 additions and 0 deletions

0
.gitignore vendored Normal file
View File

6
Gemfile Normal file
View File

@ -0,0 +1,6 @@
source 'https://rubygems.org'
gem 'tilt'
gem 'erubis'
gem 'haml'

15
Gemfile.lock Normal file
View File

@ -0,0 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
erubis (2.7.0)
haml (4.0.4)
tilt
tilt (2.0.0)
PLATFORMS
ruby
DEPENDENCIES
erubis
haml
tilt

70
compare.rb Executable file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'benchmark'
require 'erubis'
require 'haml'
require 'tilt/erb'
require 'tilt/erubis'
require 'tilt/haml'
# Terminal settings
TERMINAL_WIDTH = 60
BM_WIDTH = 46
# Benchmark settings
COMPILE_LOOPS = 1000
RENDER_LOOPS = 10000
$templates = {}
$template_params = {title: 'Greetings!', username: '%username%'}
def path_for template
File.expand_path('../templates/' + template, __FILE__)
end
def banner title
puts '#' * TERMINAL_WIDTH
puts '##' + title.center(TERMINAL_WIDTH - 4) + '##'
puts '#' * TERMINAL_WIDTH
end
ENGINES = {
erb: {
class: Tilt::ERBTemplate,
layout: path_for('erb/layout.erb')
},
erubis: {
class: Tilt::ErubisTemplate,
layout: path_for('erubis/layout.erubis')
},
haml: {
class: Tilt::HamlTemplate,
layout: path_for('haml/layout.haml')
}
}
banner 'Compilation (%d runs)' % COMPILE_LOOPS
Benchmark.bm(TERMINAL_WIDTH - BM_WIDTH) do |x|
ENGINES.each do |name, attrs|
x.report(name) do
COMPILE_LOOPS.times do
$templates[name] = attrs[:class].new(attrs[:layout])
end
end
end
end
puts
banner 'Render (%d runs)' % RENDER_LOOPS
Benchmark.bm(TERMINAL_WIDTH - BM_WIDTH) do |x|
ENGINES.each do |name, attrs|
x.report(name) do
RENDER_LOOPS.times do
$templates[name].render(Object, $template_params)
end
end
end
end

18
templates/erb/layout.erb Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1>Hello, <%= username %>!</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</p>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1>Hello, <%= username %>!</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</p>
</body>
</html>

View File

@ -0,0 +1,17 @@
!!!
%html
%head
%title= title
%body
%h1
Hello,
= username
!
%p
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.