🕷 zenspider.com

by ryan davis



sitemap
Looking for the Ruby Quickref?

ERB made faster (by about 40%?)

Published 2006-09-07 @ 14:58

Tagged rails, ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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