Created
March 15, 2012 05:22
-
-
Save ferrous26/2042104 to your computer and use it in GitHub Desktop.
Is it worth caching ActiveSupport::Inflector inflections that get used a lot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hell yes!