On Mon, 24 Mar 2003, Joshua Hoblitt wrote: > DateTime => sub { > my $dt1 = DateTime->now(); > $dt1->set_time_zone( '-1000' ); > my $dt2 = DateTime::Format::MySQL->parse_datetime( "2000-05-10 > 15:15:00" ); > my $dt_dur = DateTime::Duration->new( days => 4 ); > $dt1->subtract_duration( $dt_dur ); > my $cmp = DateTime->compare($dt1, $dt2); > },
And this could be sped up. Move making the duration object out of the sub, and make it like this: my $dt_dur = DateTime::Duration->new( days => -4 ); and inside the sub call add_duration instead. On my box, and AMD Athlon 1.33 Ghz running Debian unstable, I get: Compare dates Benchmark: running Date::Manip, DateTime for at least 5 CPU seconds... Date::Manip: 5 wallclock secs ( 5.09 usr + 0.00 sys = 5.09 CPU) @ 167.78/s (n=854) DateTime: 6 wallclock secs ( 5.28 usr + 0.00 sys = 5.28 CPU) @ 483.90/s (n=2555) Rate Date::Manip DateTime Date::Manip 168/s -- -65% DateTime 484/s 188% -- Change TZ Benchmark: running Date::Manip, DateTime for at least 5 CPU seconds... Date::Manip: 5 wallclock secs ( 5.27 usr + 0.00 sys = 5.27 CPU) @ 356.17/s (n=1877) DateTime: 5 wallclock secs ( 5.17 usr + 0.00 sys = 5.17 CPU) @ 802.71/s (n=4150) Rate Date::Manip DateTime Date::Manip 356/s -- -56% DateTime 803/s 125% -- Looks pretty decent. I added in another possibility using Date::Calc and Date::Parse (since Date::Calc's parsing doesn't do times, just dates) like this: 'Date::Calc and Date::Parse' => sub { my (@dc1) = Date::Calc::Today_and_Now(); my (@dc2) = reverse ( ( Date::Parse::strptime( "2000-05-10 15:15:00" ) )[0..5] ); $dc2[0] += 1900; my $cmp = Date::Calc::Date_to_Time(@dc1) <=> Date::Calc::Date_to_Time(@dc2); } And got this: Compare dates Benchmark: running Date::Calc and Date::Parse, Date::Manip, DateTime for at least 5 CPU seconds... Date::Calc and Date::Parse: 5 wallclock secs ( 5.20 usr + 0.02 sys = 5.22 CPU) @ 10537.93/s (n=55008) Date::Manip: 5 wallclock secs ( 5.33 usr + 0.00 sys = 5.33 CPU) @ 166.60/s (n=888) DateTime: 6 wallclock secs ( 5.25 usr + 0.01 sys = 5.26 CPU) @ 485.74/s (n=2555) Rate Date::Manip DateTime Date::Calc and Date::Parse Date::Manip 167/s -- -66% -98% DateTime 486/s 192% -- -95% Date::Calc and Date::Parse 10538/s 6225% 2069% -- Of course, Date_to_Time only works for datetimes that fit in your machine's epoch, and the solution in the Date::Calc docs for doing non-epoch times is quite ugly. Nonetheless, it'd still be nice to go faster ;) -dave /*======================= House Absolute Consulting www.houseabsolute.com =======================*/