aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-04-29 19:20:59 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-04-29 20:21:58 +0900
commit0c54fc460e52d2b9aa02e1e27a090dbe7ee98829 (patch)
treea0e119cdd2b1124b2521b40d4e76e435e681919a /activesupport
parent5f2ee4c0bb790480a7032d1779052be2e1e46808 (diff)
downloadrails-0c54fc460e52d2b9aa02e1e27a090dbe7ee98829.tar.gz
rails-0c54fc460e52d2b9aa02e1e27a090dbe7ee98829.tar.bz2
rails-0c54fc460e52d2b9aa02e1e27a090dbe7ee98829.zip
Improve the performance of `ActiveSupport::Inflector.ordinal`
This improves the performance for the most ordinalized numbers (1st, 2nd, 3rd, etc). ``` require "benchmark/ips" def o1(number) abs_number = number.to_i.abs if (11..13).include?(abs_number % 100) "th" else case abs_number % 10 when 1; "st" when 2; "nd" when 3; "rd" else "th" end end end def o3(number) case number when 1; "st" when 2; "nd" when 3; "rd" when 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; "th" else num_modulo = number.to_i.abs % 100 if 11 <= num_modulo && num_modulo <= 13 "th" else case num_modulo % 10 when 1; "st" when 2; "nd" when 3; "rd" else "th" end end end end def o4(number) case number when 1; "st" when 2; "nd" when 3; "rd" when 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; "th" else num_modulo = number.to_i.abs % 100 num_modulo %= 10 if num_modulo > 13 case num_modulo when 1; "st" when 2; "nd" when 3; "rd" else "th" end end end puts RUBY_DESCRIPTION Benchmark.ips do |x| x.report("orig") { o1(1); o1(2); o1(3); o1(4); o1(11); o1(111); o1(1523) } x.report("ord3") { o3(1); o3(2); o3(3); o3(4); o3(11); o3(111); o3(1523) } x.report("ord4") { o4(1); o4(2); o4(3); o4(4); o4(11); o4(111); o4(1523) } x.compare! end ``` ``` ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin15] Warming up -------------------------------------- orig 25.305k i/100ms ord3 121.146k i/100ms ord4 124.944k i/100ms Calculating ------------------------------------- orig 275.496k (± 2.4%) i/s - 1.392M in 5.054720s ord3 1.649M (± 5.0%) i/s - 8.238M in 5.009801s ord4 1.700M (± 7.0%) i/s - 8.496M in 5.031646s Comparison: ord4: 1700059.6 i/s ord3: 1649154.9 i/s - same-ish: difference falls within error orig: 275496.3 i/s - 6.17x slower ``` Closes #25020. [lvl0nax, Jeremy Daer, Ryuta Kamizono]
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/locale/en.rb21
1 files changed, 12 insertions, 9 deletions
diff --git a/activesupport/lib/active_support/locale/en.rb b/activesupport/lib/active_support/locale/en.rb
index 26c2280c95..a2a7ea7ae1 100644
--- a/activesupport/lib/active_support/locale/en.rb
+++ b/activesupport/lib/active_support/locale/en.rb
@@ -5,16 +5,19 @@
number: {
nth: {
ordinals: lambda do |_key, number:, **_options|
- abs_number = number.to_i.abs
-
- if (11..13).cover?(abs_number % 100)
- "th"
+ case number
+ when 1; "st"
+ when 2; "nd"
+ when 3; "rd"
+ when 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; "th"
else
- case abs_number % 10
- when 1 then "st"
- when 2 then "nd"
- when 3 then "rd"
- else "th"
+ num_modulo = number.to_i.abs % 100
+ num_modulo %= 10 if num_modulo > 13
+ case num_modulo
+ when 1; "st"
+ when 2; "nd"
+ when 3; "rd"
+ else "th"
end
end
end,