Skip to content

Instantly share code, notes, and snippets.

@tizoc
Created April 3, 2011 12:56
Show Gist options
  • Save tizoc/900410 to your computer and use it in GitHub Desktop.
Save tizoc/900410 to your computer and use it in GitHub Desktop.
require 'rubygems'
require "./nolate"
require "erb"
require "bench"
module FastERB
def self.new(*args)
o = Object.new
erb = ERB.new(*args)
o.extend erb.def_module("render(myfield)")
end
end
myfield = 10
slow_erb = ERB.new("The sum is <%=2+2%> Also substitute <%=myfield%> ok")
slow_erb100 = ERB.new("The sum is <%=2+2%> Also substitute <%=myfield%> ok" * 100)
fast_erb = FastERB.new("The sum is <%=2+2%> Also substitute <%=myfield%> ok")
fast_erb100 = FastERB.new("The sum is <%=2+2%> Also substitute <%=myfield%> ok" * 100)
benchmark "nolate" do
nolate("The sum is <%=2+2%> Also substitute <%#myfield%> ok", {:myfield => 10})
end
benchmark "slow_erb" do
slow_erb.result
end
benchmark "fast_erb" do
fast_erb.render(10)
end
benchmark "nolate * 100" do
nolate("The sum is <%=2+2%> Also substitute <%#myfield%> ok" * 100, {:myfield => 10})
end
benchmark "slow_erb * 100" do
slow_erb100.result
end
benchmark "fast_erb * 100" do
fast_erb100.render(10)
end
run 10000
# Ruby 1.8.7
# user system total real
# nolate 0.340000 0.020000 0.360000 ( 0.349417)
# slow_erb 0.410000 0.000000 0.410000 ( 0.414083)
# fast_erb 0.070000 0.000000 0.070000 ( 0.077964)
# nolate * 100 25.520000 0.760000 26.280000 ( 26.967786)
# slow_erb * 100 22.780000 0.080000 22.860000 ( 23.389305)
# fast_erb * 100 3.360000 0.010000 3.370000 ( 3.402738)
# Ruby 1.9.2
# user system total real
# nolate 0.320000 0.010000 0.330000 ( 0.333347)
# slow_erb 0.510000 0.010000 0.520000 ( 0.515306)
# fast_erb 0.030000 0.000000 0.030000 ( 0.037021)
# nolate * 100 22.060000 0.080000 22.140000 ( 22.420401)
# slow_erb * 100 19.320000 0.080000 19.400000 ( 19.461102)
# fast_erb * 100 1.580000 0.010000 1.590000 ( 1.618233)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment