This project is archived and is in readonly mode.

#6515 ✓committed
Diego Carrion

Refactors on Date#<=> and DateTime#<=>

Reported by Diego Carrion | March 4th, 2011 @ 12:02 AM | in 3.1

Hi, I was analyzing an application trace and I came to Time#<=> and DateTime#<=>. I was doing some tests and ended up refactoring the two methods. I don't know if there were much performance gain but now the code if much more legible. What do you think?

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino March 4th, 2011 @ 02:26 AM

    • Importance changed from “” to “Low”

    Hey Diego, can you benchmark that?

  • Diego Carrion

    Diego Carrion March 4th, 2011 @ 03:43 AM

    Yes, no problem, will come with the results :)

  • Diego Carrion

    Diego Carrion March 4th, 2011 @ 03:26 PM

    Ok, I executed 10000 times with Ruby 1.8.7 the tests, each with 3 asserts, that compares:

    Time with Time
    Time with DateTime
    Time with TimeWithZone
    DateTime with Time
    DateTime with DateTime
    DateTime with TimeWithZone

    and found that the call that is really expensive is Time#to_datetime .

    As the first patch called #to_datetime in both self and the argument in Time#<=>, the performance of comparing a Time with another Time got really bad, as two expensive unnecessary operations were being called. In the second attached patch I changed Time#<=> to only execute self#to_datetime if the argument is a datetime. With this change, the performance was improved in all cases but just a little bit, in a normal request it may be insignificant.

    Time#<=> DateTime

    before:

    47.57% 0.68% 28.62 0.41 0.00 28.21 30000 Time#<=> 285

         26.58   0.92    0.00    25.66   30000/30000    Time#to_datetime    290
         1.27    0.35    0.00    0.92    30000/30000    Date#compare_without_coercion   290
         0.31    0.21    0.00    0.10    30000/30000    Object#acts_like?   288
         0.05    0.05    0.00    0.00    30000/60000    Kernel#respond_to?  287
    

    after:

    47.22% 0.27% 28.49 0.17 0.00 28.33 30000 Time#<=> 280

         26.81   0.92    0.00    25.89   30000/30000    Time#to_datetime    281
         1.52    0.17    0.00    1.36    30000/30000    DateTime#<=>    281
    

    Time#<=> Time

    before:

    13.67% 6.31% 0.72 0.33 0.00 0.39 30000 Time#<=> 285

         0.30    0.21    0.00    0.10    30000/30000    Object#acts_like?   288
         0.04    0.04    0.00    0.00    30000/60000    Kernel#respond_to?  287
         0.04    0.04    0.00    0.00    30000/30000    Time#compare_without_coercion   292
    

    after:

    7.66% 4.70% 0.39 0.24 0.00 0.15 30000 Time#<=> 282

         0.07    0.07    0.00    0.00    30000/30000    Time#to_time    283
         0.04    0.04    0.00    0.00    30000/30000    Kernel#is_a?    283
         0.04    0.04    0.00    0.00    30000/30000    Time#compare_without_coercion   283
    

    Time#<=> TimeWithZone

    before:

    17.05% 5.52% 1.17 0.38 0.00 0.79 30000 Time#<=> 285

         0.39    0.26    0.00    0.13    30000/30000    ActiveSupport::TimeWithZone#respond_to? 287
         0.29    0.20    0.00    0.09    30000/30000    Object#acts_like?   288
         0.08    0.08    0.00    0.00    30000/30000    ActiveSupport::TimeWithZone#comparable_time 287
         0.04    0.04    0.00    0.00    30000/30000    Time#compare_without_coercion   292
    

    after:

    9.72% 3.77% 0.63 0.24 0.00 0.38 30000 Time#<=> 282

         0.19    0.11    0.00    0.08    30000/30000    ActiveSupport::TimeWithZone#to_time 283
         0.16    0.12    0.00    0.04    30000/30000    ActiveSupport::TimeWithZone#is_a?   283
         0.04    0.04    0.00    0.00    30000/30000    Time#compare_without_coercion   283
    
  • Santiago Pastorino

    Santiago Pastorino March 4th, 2011 @ 06:52 PM

    • State changed from “new” to “committed”
    • Milestone set to 3.1

    Pushed 7872cc992bac76e976334d0c20649d69aad5652e

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages