Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created April 20, 2011 13:04
Show Gist options
  • Save judofyr/931279 to your computer and use it in GitHub Desktop.
Save judofyr/931279 to your computer and use it in GitHub Desktop.
require 'tilt'
require 'erb'
require 'benchmark'
# http://twitter.com/tiodante/status/60685349868929024
#
# "Sinatra doesn't make ERB slow, Tilt does (and for smaller templates). ERB in
# itself is fast if used properly."
# Testing with a small template:
tpl = "Hello <%= @world %>"
class Scope
def initialize(world)
@world = world
end
def b; binding; end
end
erb = ERB.new(tpl)
erb.def_method(Scope, :cached_erb)
tilt = Tilt::ERBTemplate.new { tpl }
N = 50_000
scope = Scope.new("World")
Benchmark.bmbm do |x|
x.report("ERB") { N.times { erb.result(scope.b) } }
x.report("ERB (cached)") { N.times { scope.cached_erb } }
x.report("Tilt") { N.times { tilt.render(scope) } }
end
@judofyr
Copy link
Author

judofyr commented Apr 20, 2011

$ ruby erb.rb 
Rehearsal ------------------------------------------------
ERB            2.810000   0.030000   2.840000 (  2.877525)
ERB (cached)   0.140000   0.000000   0.140000 (  0.163492)
Tilt           0.770000   0.010000   0.780000 (  0.798502)
--------------------------------------- total: 3.760000sec

                   user     system      total        real
ERB            3.200000   0.020000   3.220000 (  3.393976)
ERB (cached)   0.130000   0.010000   0.140000 (  0.153053)
Tilt           0.750000   0.000000   0.750000 (  0.758204)

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