r/cpp • • 25d ago

A faster way to calculate the date of Easter

https://www.benjoffe.com/fast-easter
49 Upvotes

2 comments sorted by

6

u/VinnieFalco wg21.org | corosio.org 25d ago

Great article! Curious to hear Howard Hinnant's thoughts on this

12

u/HowardHinnant 24d ago edited 24d ago

Looks good to me. Here's my attempt at this functionality using chrono: https://stackoverflow.com/q/79593231/576911

Ben's implementation handles negative years, mine doesn't. Ben's implementation avoids %19, mine doesn't. I haven't benchmarked the two. But I basically just copied my algorithm from Calendrical Calculations as noted at the above link. I'd bet money Ben's work on optimizations has produced faster code than what I posted.

Nice read Ben!

Fwiw, curiosity got the best of me, and I tested my chrono implementation against Ben's over the range of years [0, 32767], which is a tiny slice of the range of validity of Ben's implementation.

Correctness: Both implementations get the same answers over this range.

Performance: I didn't do any timings, but I looked at the assembly at -O3. Ben's assembly is significantly shorter than mine, and is branchless. Mine has two branches. Neither has loops. Both are going to be fast. But Ben's will be faster. Not sure by how much, but I'm sure by a healthy percentage.

Disclaimer: I only tested Ben's "return day index" version.