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.)
> Will switching to YAML fixtures make this faster?It doesn''t seem like it. I just ran this test: 1000.times do YAML::load "date: 2005-12-12" end Envy:~ david$ ruby -r yaml -r profile test.rb % cumulative self self total time seconds seconds calls ms/call ms/call name 33.28 18.69 18.69 3000 6.23 9.12 Integer#gcd 6.82 22.52 3.83 71000 0.05 0.05 Fixnum#= 6.11 25.95 3.43 3000 1.14 11.69 Rational#reduce 5.77 29.19 3.24 1000 3.24 5.59 Date#jd_to_civil 5.04 32.02 2.83 5000 0.57 0.74 Rational#initialize 4.31 34.44 2.42 45000 0.05 0.05 Fixnum#[] 3.95 36.66 2.22 1000 2.22 4.19 Date#civil_to_jd 3.01 38.35 1.69 2000 0.85 7.94 Rational#/ 2.48 39.74 1.39 23000 0.06 1.27 Fixnum#- 2.14 40.94 1.20 19000 0.06 0.06 Kernel.kind_of? Seems like its inherent in dealing with dates. I wonder if you can turn of reducing action for a while. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain