ERB made faster (by about 40%?)

| | Comments (1)
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:

erb.png

1 Comments

Impressive. I assume you are going to submit a patch, right? Rails folks will thank you.

Leave a comment

About this Entry

This page contains a single entry by zenspider published on September 7, 2006 2:58 PM.

ruby quine _slightly_ smaller than python quine was the previous entry in this blog.

Coming Up for Air is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.32-en