Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DateTime::Tiny (and Date::Tiny and Time::Tiny) for improved performance #6278

Open
ehuelsmann opened this issue Dec 12, 2021 · 1 comment
Labels
performance Issue identifies a potential performance problem or category type:enhancement Proposed improvement, new feature, or extension -- not a defect

Comments

@ehuelsmann
Copy link
Member

The majority of dates don't need calculation (just (de)serialization and user-formatting). DateTime is awfully slow at (de)serializing, although it does it's calculations as correct as possible. For (de)serialization alone, Date(Time)::Tiny are much more suited:

  use Benchmark qw(:all);
  use DateTime;
  use DateTime::Format::Strptime;
  use DateTime::Tiny;


my $format = DateTime::Format::Strptime->new(
      pattern => '%F'
);

timethese(100000, {
   'DateTime' => sub { $format->parse_datetime('2020-01-01'); },
   'DateTime::Tiny' => sub { DateTime::Tiny->from_string('2020-01-01T00:00:00'); }
});


my $dt = $format->parse_datetime('2020-01-01');
my $dtt = DateTime::Tiny->from_string('2020-01-01T00:00:00');


timethese(100000, {
   'DateTime' => sub { $format->format_datetime($dt); },
   'DateTime::Tiny' => sub { $dtt->as_string; }
});

prints:

Benchmark: timing 100000 iterations of DateTime, DateTime::Tiny...
  DateTime:  8 wallclock secs ( 7.88 usr +  0.00 sys =  7.88 CPU) @ 12690.36/s (n=100000)
DateTime::Tiny:  0 wallclock secs ( 0.52 usr +  0.00 sys =  0.52 CPU) @ 192307.69/s (n=100000)
Benchmark: timing 100000 iterations of DateTime, DateTime::Tiny...
  DateTime:  2 wallclock secs ( 1.31 usr +  0.00 sys =  1.31 CPU) @ 76335.88/s (n=100000)
DateTime::Tiny:  0 wallclock secs ( 0.12 usr +  0.00 sys =  0.12 CPU) @ 833333.33/s (n=100000)
            (warning: too few iterations for a reliable count)

Changing "timethese" to "cmpthese", the output is:

                   Rate       DateTime DateTime::Tiny
DateTime        12438/s             --           -94%
DateTime::Tiny 196078/s          1476%             --
            (warning: too few iterations for a reliable count)
                   Rate       DateTime DateTime::Tiny
DateTime        80645/s             --           -91%
DateTime::Tiny 909091/s          1027%             --
@ehuelsmann ehuelsmann added the performance Issue identifies a potential performance problem or category label Dec 12, 2021
@einhverfr
Copy link
Member

It should be pretty trivial to do a PGObject::Type::DateTime::Tiny and then inherit from that. The one thing is infinity and -infinity would probably need some special handling. In DateTime these are basically immutable singletons which makes sense.

@neilt neilt added the type:enhancement Proposed improvement, new feature, or extension -- not a defect label Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Issue identifies a potential performance problem or category type:enhancement Proposed improvement, new feature, or extension -- not a defect
Projects
None yet
Development

No branches or pull requests

3 participants