A walkthrough creating a new date-time algorithm from the ground up, optimizing it along the way.| jhpratt.dev
bool is_leap_year3(uint32_t y) { return (y & ((y % 25) ? 3 : 15)) == 0; }| godbolt.org
bool is_leap_year1(uint32_t y) { if ((y % 4) != 0) return false; if ((y % 25) != 0) return true; if ((y % 16) == 0) return true; return false; }| godbolt.org
One of my colleagues was recently looking at some date-based calculations that needed to know whether a year is a leap year or not, on the hot path, and he found an 8% speed improvement by reworking the leap year predicate a bit. The JVM compiles this down to a few fast integer operations, avoiding slow things like division entirely. I thought it’d be interesting to dig into how to calculate the leap year predicate without division.| David Turner says…
Wolfram|Alpha brings expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels.| www.wolframalpha.com