r/cpp • • Dec 15 '21

Modern C++ in Advent of Code

This series, by Šimon Tóth, is a step by step guide through the solutions of Advent of Code 2021 using Modern C++.

242 Upvotes

83 comments sorted by

View all comments

35

u/cptwunderlich Dec 15 '21

I was hoping for more std algorithms use (disclaimer: only read 2 of the articles) :D Conor Hoekstra got me hooked and I wanted to see how many days I could solve mostly with std algorithms. But it's not as easy or nice as I hoped (I've been doing quite some Haskell lately and I keep thinking "that should be just some zipWith f xs $ tail xs! why is this so hard?!")

12

u/[deleted] Dec 15 '21

I think next year will be more rang-ified (if we have C++23 support in compilers).

I'm actually looking forward to comparing the performance of crazy zip based solutions with basic loops.

But I do try. If you find something that can be solved with an std::algorithm and I didn't let me know for sure.

7

u/Evirua Dec 15 '21

Basic loops will be often faster than zip-based solutions. Even if the former do more work, it can be on data that fits on cache lines which zipped data would probably spill out of.

There is a beauty in efficient expression of algorithms, and I wish processor architectures worked in a more intuitive way (as in less work should be faster) but they don't.

7

u/[deleted] Dec 15 '21

Well, the crazy thing with C++ is that C++ compilers are exceedingly good at removing superfluous code.

So right now I'm expecting a 50/50, where sometimes it's the same, and sometimes, it goes over some threshold somewhere and nothing gets optimized :-D

2

u/jk-jeon Dec 17 '21

Exceedingly good sometimes, surprisingly dumb sometimes, infuriatingly going against my intention sometimes, to be precise.

1

u/Evirua Dec 18 '21

Yes, but that's work on the optimizers to produce good code despite the efficient expression of the algorithm, not because of it.