r/cpp • u/ReDucTor Game Developer | quiz.cpp-perf.com • Jun 03 '26
C++ Performance Quiz - A small side project to test your intuition for slow code
https://quiz.cpp-perf.com/I have been working on this little side project in my spare time and finally ready to start putting it online.
It's a C++ Performance Quiz, covering a bunch of topics from algorithms to general unexpected performance gotchas. Every question includes a compiler explorer link so you can look at the actual machine code the compiler generated once you pick an answer.
The goal is for people to have fun and help build a bit of intuition about performance; answering correctly or incorrectly is not intended to reflect someone's skill.
All feedback is welcome.
EDIT: Thanks everyone for the feedback, I plan to address a bunch of the issues raised over the weekend.
29
u/SubjectiveMouse Jun 03 '26 edited Jun 03 '26
Many questions are too x86-specific. Others are outright wrong (i.e. question 13 would generate identical code in most real-world situations)
upd. question 85 is wrong. Simply rerunning the example code on godbolt shows different results. Runtime is dominated by function call cost, so the result is just noise
9
u/LucHermitte Jun 03 '26
Regarding the switch VS lookup table (#85?), clang 17 and GCC have similar performances (if not better?) for the
switchimplementation. https://quick-bench.com/q/MgPgpZApNjHg0Uey2Taf_T08oYQBTW, gcc is able to transform the non-tail recursion into a loop.
3
u/meetingcpp Meeting C++ | C++ Evangelist Jun 03 '26
Are you sure with the first question?
struct Particle {
};
This ain't 32 bytes: https://godbolt.org/z/ehca9x15T
Thanks to Peter Sommerlad for the hint!
4
u/Dragdu Jun 03 '26
Compilers could do it, but in practice the singular actually implemented optimization for atomics is DCE for function-local atomics that obviously don't escape.
2
u/Dragdu Jun 04 '26 edited Jun 04 '26
Wait, are the benchmarks actually run on CE? That'sterribleidea, the execution is bursted and will wildly vary between runs.They are not, see the reply.
2
u/_Noreturn Jun 07 '26
Nice website. I will definitely try completing them, how long did this take you?
27
u/adrian17 Jun 03 '26
Popcount question got me: I knew for a fact that Clang has popcount idiom recognition (and I thought you were testing for that knowledge), but I did not expect that it's not able to recognize the specific one you used in your example.
In general, a couple of these aren't really "which one is faster" but "is the compiler smart enough by now to make both equally fast".
12
u/tialaramex Jun 03 '26
The "Which snippet?" questions should refer to the clearly labelled "SNIPPET A" versus "SNIPPET B", or, those titles should be replaced so that they match the question.
And yeah, like /u/SubjectiveMouse says this cares too much about the specific hardware. I didn't see examples yet where I thought the answer was "wrong" but I did see some where I felt like the easy take away isn't building a good intuition.
Question 5's "Highlight" doesn't seem to work?
7
u/Stevo15025 Jun 03 '26
Very cool! Only feedback is that you should rename the 'Shared test data' to something else like 'Data Context' and have it expanded by default. I missed the simple linear scan one vs binary search because I did not open the drop down and did not see the size of N
6
u/Nicksaurus Jun 03 '26 edited Jun 03 '26
This is a really nice project. I got 92/100. I have issues with some of the questions though
noinline, otherwise they both optimise to the same codeAlso some of the questions are a bit too obvious e.g.:
Edit: Actually, one more comment. I liked how many of these answers depended on the data they run on. In the real world there's often no universally fastest algorithm, you have to make the right choice for your data
1
u/Dragdu Jun 04 '26
The explanation for 39 is very wrong. The big difference isn't in the loop dependency allowing multiple single-
floatdouble addition on its own, it is in the fact that the second form can be turned in 4-wideaddpdby the compiler, while the first one cannot.1
u/pavel_v Jun 28 '26
Very cool idea! Just a note: Snippet A from question 78 has the same code fragment repeated twice.
6
u/tjrileywisc Jun 03 '26
How much time writing c++ is necessary before one gains an intuition about this? I continue to be humbled about how little I know after 4+ years of writing in it primarily, but I also haven't been using the STL much or need to focus intensely on writing the most performant code.
3
u/Jovibor_ Jun 03 '26
Is there a chance to have a normal, light theme?
Not everyone is a fan of that bleeding eyes dark horror.
2
u/trad_emark Jun 03 '26
> All feedback is welcome.
please split them into categories: data structures, atomics, etc.. 😉
2
u/lonkamikaze Jun 03 '26
Note in the first example you're padding
struct Particleto 20 bytes, not 32.2
u/celestabesta Jun 03 '26
Regarding question 5, is the compiler not allowed to optimize that copy away? Why doesn't it?
2
u/SufficientBet1834 Jun 06 '26
love it
2
u/thatsureisafinefish Jun 08 '26
Super cool! The questions where "both compile to the same thing" are actually the most useful ones. Knowing that the compiler will bail you out is part of the intuition too.
2
u/kirgel Jun 03 '26
Great job! I’m on question 5 at the moment and enjoying it. Will come back to add feedback as I go.
For Q5, wouldn’t the result be affected by small string optimization? It wasn’t immediately clear to me whether that was taken into account from the test data and explanation.
2
u/mike_kazakov Jun 03 '26
Great stuff, thanks! 86/100, was surprised many times 😄
1
u/Jovibor_ Jun 03 '26
This is incorrect.
On most platforms `float` is 4 byte, so the struct size will be 20 bytes after padding, not 32.
1
u/joaquintides Boost author Jun 03 '26
Excellent project! Thanks for sharing
1
u/cmake-advisor Jun 03 '26
I got 67/100. Most of the ones I got wrong were due to not knowing data structures. I don't even know what a red-black tree is, nevermind why it's more or less performant than hash computation/comparisons in certain contexts.
1
u/Dghelneshi Jun 03 '26
The explanation for #75 (Pixel Write and Readback Patterns) misses the point entirely. The actual question is why the compiler cannot turn snippet B into snippet A. It's quite obvious that one store instruction is cheaper than multiple stores, so the store-to-load forwarding explanation didn't convince me that it's actually important here. Then I looked at the disassembly and it turns out the compiler does not even emit a load for the packed store in the entire loop because the value is already known and in a register, which means hardware-based forwarding is truly irrelevant and thus the wrong explanation.
I also personally dislike how many of these feel like trick questions because they are so blindingly obvious. The equivalent of "is doing more work slower than doing less work" or "is giving the compiler incorrect information slower than giving it correct information" occasionally made me pick the wrong answer because I thought it must be something unintuitive because otherwise why even ask the question. I understand that most of those questions are intended to be like "hey, did you know language/library feature XYZ exists?", but in some ways it feels like a quiz is the wrong tool for that, but maybe that's just me. There were also a few questions that were essentially the same as others and felt like padding just to get a round number of 100.
-4
u/Ateist Jun 03 '26
Why on earth would anyone care about any of that?!
You solve problems when you see them in Profiler - not any earlier.
-1
u/cr1mzen Jun 03 '26
I would disagree with the premise that one should first reach for intuition. This is based on years of blowhards offering plain wrong advice based on hunches and old wives tales.
0
u/[deleted] Aug 08 '26
[removed] — view removed comment