Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created May 12, 2009 14:05
Show Gist options
  • Save speedmax/110500 to your computer and use it in GitHub Desktop.
Save speedmax/110500 to your computer and use it in GitHub Desktop.
[ /workspace/h2o ] >: ruby benchmark/parser.rb
/
(.*?)(?:
\{% (.*?)
%\} |
\{\{ (.*?)
\}\} |
\{\* (.*?)
\*\} | (.*?)
)(?:\r?\n)
/mx
user system total real
String#scan : 0.070000 0.000000 0.070000 ( 0.077598)
String#scan - with \G : 0.060000 0.000000 0.060000 ( 0.059510)
String#scan - optimized : 0.030000 0.000000 0.030000 ( 0.034286)
String#scan - optimized with \G : 0.040000 0.010000 0.050000 ( 0.049830)
require 'rubygems'
require 'benchmark'
require File.dirname(__FILE__)+'/../lib/h2o'
require 'stringio'
require 'pp'
require 'strscan'
ParseRegex = /
(.*?)(?:
#{Regexp.escape(H2o::BLOCK_START)} (.*?)
#{Regexp.escape(H2o::BLOCK_END)} |
#{Regexp.escape(H2o::VAR_START)} (.*?)
#{Regexp.escape(H2o::VAR_END)} |
#{Regexp.escape(H2o::COMMENT_START)} (.*?)
#{Regexp.escape(H2o::COMMENT_END)} | (.*?)
)(?:\r?\n)
/xm
ParseRegex2 = /\G
(.*?)(?:
#{Regexp.escape(H2o::BLOCK_START)} (.*?)
#{Regexp.escape(H2o::BLOCK_END)} |
#{Regexp.escape(H2o::VAR_START)} (.*?)
#{Regexp.escape(H2o::VAR_END)} |
#{Regexp.escape(H2o::COMMENT_START)} (.*?)
#{Regexp.escape(H2o::COMMENT_END)}
) (?:\r?\n)
/xm
ParseRegex3 = / (?:\{\{ .*? \}\}|.*?|\{(?:% .*? %|\* .*? \*)\}) \r?\n /mx
ParseRegex4 = /\G (?:\{\{ .*? \}\}|.*?|\{(?:% .*? %|\* .*? \*)\}) \r?\n /mx
file = File.read(File.dirname(__FILE__)+'/../benchmark/source.html')
pp ParseRegex
Benchmark.bm do|b|
b.report('String#scan :') do
file.scan(ParseRegex)
end
b.report('String#scan - with \G :') do
file.scan(ParseRegex2)
end
b.report('String#scan - optimized :') do
file.scan(ParseRegex3)
end
b.report('String#scan - optimized with \G :') do
file.scan(ParseRegex4)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment