This post originated from an RSS feed registered with Ruby Buzz
by Ryan Davis.
Original Post: ERB made faster (by about 40%?)
Feed Title: Polishing Ruby
Feed URL: http://blog.zenspider.com/index.rdf
Feed Description: Musings on Ruby and the Ruby Community...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Ryan Davis
Latest Posts From Polishing Ruby
Advertisement
require 'benchmark'
require 'erb'
max = (ARGV.shift || 1_000_000).to_i
def validate(src)
expect = "blah blah 2 " * 50
result = eval(src)
unless expect == result then
raise "expected\n<#{expect}>\nbut got\n<#{result}>\n"
end
end
def run
iters = [10, 100, 1_000, 10_000, 100_000]
Hash[*iters.map { |max|
real = Benchmark::measure do
src = []
50.times { src << "blah blah <%= 1 + 1 %> " }
src = src.join
src = ERB.new(src).src
validate src
for i in 0..max do
eval src
end
end.real
[max, real]
}.flatten]
end
before = run
require 'make_erb_fast' # patches 3 methods
after = run
before.keys.sort.each do |iter|
puts "%6d\t%6.4f\t%6.4f" % [iter, before[iter], after[iter]]
end
begets:
Read: ERB made faster (by about 40%?)