def escapeHTML(string)
string.gsub(/&/n, '&').gsub(/\"/n, '"').gsub(/>/n, '>').gsub(/</n, '<')
end
ESC = {
'&' => '&',
'"' => '"',
'<' => '<',
'>' => '>'
}
def escape(str)
table = ESC # optimize
str.gsub(/[&"<>]/n) {|s| table[s] }
end
require 'benchmark'
n = (ARGV[0] || 1).to_i
s = 'aaabbbbaaa'
Benchmark.bm(16) {|x|
x.report('cgi.rb') {
n.times do
escapeHTML(s)
end
}
x.report('aamine') {
n.times do
escape(s)
end
}
}