mldvx4 has asked for the wisdom of the Perl Monks concerning the following question:

Would this algorithm by Ben Joffe be something useful for Date::Calc or Time::Piece as is?

A Very Fast 64–Bit Date Algorithm: 30-40% faster.

It apparently gains speed by counting backwards and doing fewer multiplications than the earlier algorithms.

Replies are listed 'Best First'.
Re: A Very Fast 64–Bit Date Algorithm
by ikegami (Patriarch) on Nov 26, 2025 at 21:30 UTC

    Since Time::Piece always works from an epoch time, the algorithm could help it assuming it doesn't slow down also getting hours, minutes and seconds.

    That said, you could get far larger savings by converting Time::Piece to C.

Re: A Very Fast 64–Bit Date Algorithm
by ysth (Canon) on Nov 26, 2025 at 21:35 UTC
    Almost anything in perl is going to have costs that dwarf the improvements he made there. But in answer to your question: look at the module sources and see what you think?
Re: A Very Fast 64–Bit Date Algorithm
by cavac (Prior) on Dec 01, 2025 at 10:04 UTC

    Hmm, the slowdown for generic date/time calculations doesn't really come from the algorithm itself, but from the idiosyncracies of humans. Timezones shift, date calculations get all kinds of weird updates, there is all the summer/winter time kerfuffle, leap seconds, etc.

    To name one of my favourite examples, the algorithm you posted will most likely fail for Samoa dates because of 2011. In 2011, Samoa skipped the 30th of December entirely, it went directly from the 29th to the 31st. That's why date/time modules are a mess of spagetti code that does all kinds of (slow'ish) lookups in many different tables, and some expensive calculations.

    With date and time calculations, it's usually more important to get them correct then to get them fast. Some timestamps might even be impossible to get correct precisely, namely those in the future. A politically motivated change to a timezone here, a drop of daylightsaving time there, an updated Bulletin C thrown in the mix and your predicted timestamp completely goes down the drain...

    You might also watch Tom Scott's video "The problem with time & timezones".

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
    Also check out my sisters artwork and my weekly webcomics

      You might also watch Tom Scott's video "The problem with time & timezones".

      Thanks. That was interesting to listen to. I had realized time was a complex task, but I was off by about three or four orders of magnitude regarding that complexity. It's very far from a calculation and more like a detailed set of lookup tables combined with a few algorithms.