Show HN: A faster day-count to Y,M,D algorithm – 30-40% speedup

Converting that integer to a Year, Month and Day is a fundamental calculation made in almost all programming languages and standard libraries. It runs every time a date is displayed, whether it's in a user interface, log, API etc.

While it is rarely a bottleneck, it is a very frequently run algorithm, and thus has attracted a lot of attention over the years.

In 2021, Cassio Neri and Lorenz Schneider developed the Neri-Schneider algorithm, which was a major breakthrough in this niche area.

Lately, I have been studying this function deeply, and found what I believe may be the last remaining substantial improvements achievable on current hardware. The gains come mainly from counting dates backwards, and from reducing the number of multiplications from 7 to 4.

Adoption will depend on whether library maintainers consider the performance benefit to outweigh the readability. If this is rejected on readability grounds, then in those cases my algorithm outlined in Article 1 (linked in the blog post navigation) is likely to be simpler and faster than what almost all existing libraries are currently doing.

I found this a very interesting challenge to work on, and I believe some of the techniques outlined may be applicable to other areas of optimisation.

3 points benjoffe 11 months ago 0 comments

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton