Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Forked from fxn/gist:1798985
Created February 12, 2012 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/1805359 to your computer and use it in GitHub Desktop.
Save tenderlove/1805359 to your computer and use it in GitHub Desktop.
require 'active_support/inflector'
require 'benchmark'
# QUICK HACK
class RuleSet
def initialize
@rules = []
@regexp = nil
end
def prepend_rule(pattern, replacement)
@rules.unshift([pattern, replacement])
patterns = @rules.each_with_index.map {|rule, i| build_regexp(rule.first, i)}
@regexp = Regexp.union(*patterns)
end
def build_regexp(pattern, i)
pattern = Regexp.quote(pattern) if pattern.is_a?(String)
/(?<_#{i}>#{pattern})/
end
def apply(word)
word = word.to_s.dup
if md = @regexp.match(word)
name = md.names.detect {|n| md[n]}
index = name[/\d+/, 0].to_i
word.gsub!(@rules[index][0], @rules[index][1])
end
word
end
end
rs = RuleSet.new
ActiveSupport::Inflector::Inflections.instance.plurals.each do |pattern, replacement|
rs.prepend_rule(pattern, replacement)
end
words = [
("x" * 7) + "bus",
("x" * 97) + "bus",
("x" * 9997) + "bus",
("x" * 999997) + "bus",
]
Benchmark.bm(11) do |x|
words.each do |word|
x.report("AS: #{word.length}") {
ActiveSupport::Inflector.pluralize(word)
}
x.report("RS: #{word.length}") {
rs.apply(word)
}
end
end
__END__
user system total real
AS: 10 0.000000 0.000000 0.000000 ( 0.000471)
RS: 10 0.000000 0.000000 0.000000 ( 0.000141)
AS: 100 0.000000 0.000000 0.000000 ( 0.000370)
RS: 100 0.000000 0.000000 0.000000 ( 0.000383)
AS: 10000 0.000000 0.000000 0.000000 ( 0.005341)
RS: 10000 0.040000 0.000000 0.040000 ( 0.038294)
AS: 1000000 0.530000 0.000000 0.530000 ( 0.540828)
RS: 1000000 3.620000 0.000000 3.620000 ( 3.654196)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment