It can goes further if using a hash instead of a case statement:

#other new behavior

ORDS = { 1 => 'st', 2 => 'nd', 3 => 'rd' }
ORDS.default = 'th'
ORDS.freeze
def ordinal3(number)
  tmp = number.to_i.abs % 100
  if 11 <= tmp && tmp <= 13
    'th'.freeze
  else
    ORDS[tmp % 10].freeze
  end
end





require 'benchmark/ips'
#current_behaviour
def ordinal(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

#new behaviour
def ordinal2(number)
  tmp = number.to_i.abs % 100

  if 11 <= tmp && tmp <= 13
    "th".freeze
  else
    case tmp % 10
      when 1; "st".freeze
      when 2; "nd".freeze
      when 3; "rd".freeze
      else    "th".freeze
    end
  end
end

ORDS = { 1 => 'st', 2 => 'nd', 3 => 'rd' }
ORDS.default = 'th'
ORDS.freeze

#other new behavior
def ordinal3(number)
  tmp = number.to_i.abs % 100
  if 11 <= tmp && tmp <= 13
    'th'.freeze
  else
    ORDS[tmp % 10].freeze
  end
end

Benchmark.ips do |x|
  x.report('before')   { ordinal(1523) }
  x.report('after 1')    { ordinal2(1523) }
  x.report('after 2')    { ordinal3(1523) }

  x.compare!
end


Warming up --------------------------------------
              before    79.375k i/100ms
             after 1    85.221k i/100ms
             after 2    98.724k i/100ms
Calculating -------------------------------------
              before      2.121M (± 6.8%) i/s -     10.557M
             after 1      2.613M (± 6.8%) i/s -     13.039M
             after 2      3.489M (± 8.3%) i/s -     17.375M

Comparison:
             after 2:  3488612.6 i/s
             after 1:  2613033.0 i/s - 1.34x slower
              before:  2120826.3 i/s - 1.64x slower


On Saturday, May 14, 2016 at 1:22:18 AM UTC-7, Jeremy Daer wrote:
>
> 👍 PR welcome.
> On Sat, May 14, 2016 at 00:14 Dmitry Gautsel <lvl...@gmail.com 
> <javascript:>> wrote:
>
>> Hi.
>>
>> What do you think about these changes:
>>
>> #current_behaviourdef ordinal(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
>>   endend
>> #new behaviourdef ordinal2(number)
>>   tmp = number.to_i.abs % 100
>>
>>   if 11 <= tmp && tmp <= 13
>>     "th".freeze
>>   else
>>     case tmp % 10
>>       when 1; "st".freeze
>>       when 2; "nd".freeze
>>       when 3; "rd".freeze
>>       else    "th".freeze
>>     end
>>   endend
>> Benchmark.ips do |x|
>>   x.report('before')   { ordinal(1523) }
>>   x.report('after')    { ordinal2(1523) }
>>   x.compare!end
>>
>> Calculating -------------------------------------
>>               before   111.577k i/100ms
>>                after   143.646k i/100ms
>> -------------------------------------------------
>>               before      3.395M (± 1.3%) i/s -     17.071M
>>                after      5.088M (± 1.1%) i/s -     25.569M
>>
>> Comparison:
>>                after:  5088264.3 i/s
>>               before:  3394612.5 i/s - 1.50x slower
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby on Rails: Core" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to rubyonrails-co...@googlegroups.com <javascript:>.
>> To post to this group, send email to rubyonra...@googlegroups.com 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/rubyonrails-core.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to