Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
def bench words
rs = RuleSet.new
ActiveSupport::Inflector::Inflections.instance.plurals.each do |pattern, replacement|
rs.prepend_rule(pattern, replacement)
end
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
def words match_word
words = [10, 100, 10000, 1000000].map { |i|
("x" * (i - match_word.length)) + match_word
}
end
puts "best case"
bench words "zombies"
puts
puts "worst case"
bench words ""
__END__
best case
user system total real
AS: 10 0.000000 0.000000 0.000000 ( 0.000297)
RS: 10 0.000000 0.000000 0.000000 ( 0.000169)
AS: 100 0.000000 0.000000 0.000000 ( 0.000181)
RS: 100 0.010000 0.000000 0.010000 ( 0.000382)
AS: 10000 0.000000 0.000000 0.000000 ( 0.000253)
RS: 10000 0.030000 0.000000 0.030000 ( 0.040468)
AS: 1000000 0.010000 0.000000 0.010000 ( 0.010319)
RS: 1000000 3.590000 0.010000 3.600000 ( 3.733272)
worst case
user system total real
AS: 10 0.000000 0.000000 0.000000 ( 0.000351)
RS: 10 0.000000 0.000000 0.000000 ( 0.000087)
AS: 100 0.000000 0.000000 0.000000 ( 0.000283)
RS: 100 0.000000 0.000000 0.000000 ( 0.000465)
AS: 10000 0.010000 0.000000 0.010000 ( 0.009323)
RS: 10000 0.040000 0.000000 0.040000 ( 0.045847)
AS: 1000000 0.940000 0.000000 0.940000 ( 0.976870)
RS: 1000000 4.380000 0.010000 4.390000 ( 4.550919)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.