Skip to content

Instantly share code, notes, and snippets.

@ferrous26
Created March 15, 2012 05:22
Show Gist options
  • Save ferrous26/2042104 to your computer and use it in GitHub Desktop.
Save ferrous26/2042104 to your computer and use it in GitHub Desktop.
Is it worth caching ActiveSupport::Inflector inflections that get used a lot
require 'benchmark'
Benchmark.bmbm do |bench|
regexp = /s$/i
empty = '' hash = {
'boxes' => 'box',
'windows' => 'window', 'buttons' => 'button',
'Buttons' => 'button',
'dialogs' => 'dialog'
}
n = 1_000_000
s = 'buttons'
bench.report('match a regular expression') do
n.times do
s.gsub! regexp, empty
end
end
bench.report('perform a hash lookup') do
n.times do
hash[s]
end
end
end
@ferrous26
Copy link
Author

Rehearsal --------------------------------------------------------------
match a regular expression   4.150000   0.080000   4.230000 (  3.850578)
perform a hash lookup        0.080000   0.000000   0.080000 (  0.081487)
----------------------------------------------------- total: 4.310000sec

                                 user     system      total        real
match a regular expression   4.370000   0.090000   4.460000 (  4.026845)
perform a hash lookup        0.090000   0.000000   0.090000 (  0.083497)

Hell yes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment