Created
August 5, 2010 19:32
-
-
Save myronmarston/510259 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
gem 'actionpack', '2.1.0' | |
gem 'activesupport', '2.1.0' | |
Kernel.module_eval do | |
alias :old_require :require | |
def require(file) | |
old_require(file) unless file == 'html/document' | |
end | |
end | |
require 'erb' | |
require 'active_support' | |
require 'action_view' | |
require 'benchmark' | |
TEMPLATE = "<b><%= name %></b> in <%= city %>, <%= state_id %>" | |
TEMPLATE_LAMBDA = lambda { |opt| "<b>#{opt[:name]}</b> in #{opt[:city]}, #{opt[:state_id]}" } | |
LOCALS = { :name => 'Wes', :city => 'Seattle', :state_id => 'WA' } | |
def render_with_action_view | |
ActionView::Base.new.render( | |
:inline => TEMPLATE, | |
:layout => false, | |
:locals => LOCALS | |
) | |
end | |
def render_with_plain_erb | |
local_variables = Struct.new(*LOCALS.keys).new(*LOCALS.values) | |
ERB.new(TEMPLATE).result(local_variables.instance_eval { binding }) | |
end | |
LOCAL_VARIABLE_CLASS_CACHE = Hash.new { |h, k| h[k] = Struct.new(*k) } | |
def render_with_plain_erb_and_class_caching | |
local_variables = LOCAL_VARIABLE_CLASS_CACHE[LOCALS.keys].new(*LOCALS.values) | |
ERB.new(TEMPLATE).result(local_variables.instance_eval { binding }) | |
end | |
def render_with_lambda | |
TEMPLATE_LAMBDA.call(LOCALS) | |
end | |
times = 10000 | |
Benchmark.bm do |bm| | |
bm.report("ERB rendering using ActionView") do | |
times.times { render_with_action_view } | |
end | |
bm.report("ERB rendering using just ERB with no class cache") do | |
times.times { render_with_plain_erb } | |
end | |
bm.report("ERB rendering using just ERB and a class cache") do | |
times.times { render_with_plain_erb_and_class_caching } | |
end | |
bm.report("Using a lambda") do | |
times.times { render_with_lambda } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[VM] ~/dev/web/wpn_rails/test(bug_32965)$ ruby erb_rendering_benchmarks.rb | |
user system total real | |
ERB rendering using ActionView 0.680000 0.150000 0.830000 ( 0.836579) | |
ERB rendering using just ERB with no class cache 3.290000 0.220000 3.510000 ( 3.504252) | |
ERB rendering using just ERB and a class cache 1.800000 0.200000 2.000000 ( 2.008031) | |
Using a lambda 0.020000 0.020000 0.040000 ( 0.045059) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment