[Rails] Integer#gcd makes tests go slow

Eric Hodel drbrain at segment7.net
Thu Dec 2 06:40:49 GMT 2004


Creating Date objects is very slow because the Date is stored as a 
Rational.  Rationals automatically get reduced, which is a very 
expensive operation:

$ ruby -r profile -Ilib:test test/unit/emailer_test.rb
   %   cumulative   self              self     total
  time   seconds   seconds    calls  ms/call  ms/call  name
  18.37     5.57      5.57      462    12.06    19.42  Integer#gcd
...
   3.30    14.73      1.00      770     1.30     1.68  
Rational#initialize
...
   2.28    17.11      0.69      462     1.49    23.59  Rational#reduce
...
   2.04    18.40      0.62      154     4.03     6.17  Date#civil_to_jd
...
   1.15    21.33      0.35       77     4.55     7.92  Date#jd_to_civil
...
   1.02    22.94      0.31      308     1.01    15.62  Rational#/

Totals to 25.88%, just for entries with > 1% of the runtime

Will switching to YAML fixtures make this faster?

(We mostly use a time_t to store dates, and convert by hand.)



More information about the Rails mailing list