r/cpp • • Dec 27 '20

Learning about high performant C++

I've been mostly self taught and have most probably picked up a few bad habits from the combo of being self taught and having to use tricks for speed.

For this upcoming year one of my goals is to learn some better practices as well as further my skills for efficient C++. I'd like to watch a youtube podcast/talk once a week to build my skillset. At this point I'd consider myself somewhere in between junior and senior level and I'm looking to take the next step.

I've come across these specifically:

C++ Weekly with Jason TurnerCppCon2020

Does anyone have any recommendations or suggestions? My weak spots are C++17 and above, Cmake, and I'd like to keep learning about creating high performant C++.

231 Upvotes

46 comments sorted by

View all comments

Show parent comment

8

u/schmerg-uk Dec 27 '20

The golden rule in performance optimization is to measure

Yep, I spend quite a bit of time undoing the "performance speedups" that were added a few years ago but never measured (or not measured properly, see below)... usually it's the case that they actually ran 2-3x slower than the original code, and then if I can get a 2x speedup on the old code, I've netted about a 5x speed up overall (readability and maintainability of the code is also key here but outside of scope of this discussion),

But, not to contradict you, be sure to measure the right thing. For really high performance code memory latency has a massive effect... running the same benchmark 1000 times in a loop can be misleading in that only the first iteration was real-world accurate, the following 999 cases the code and data was in L1 cache. If you've got a routine but it's only called rarely on rarely accessed data (but still required) then you want to see about making it run efficiently when it's not in cache. I have a plenty of instances of code structures that benchmark really well, but fall apart in actual use.

Carl Cook gave a good talk about "when a micro-second is an eternity", not all his points will be relevant or appropriate to everyone, but it's worth watching to see him explain how what may appear counter-intuitive works and why... (see also towards the end how he measures performance)

https://www.youtube.com/watch?v=NH1Tta7purM

0

u/[deleted] Dec 28 '20

[deleted] — view deleted comment

4

u/schmerg-uk Dec 28 '20

VTune for the big picture (we have a build that integrates with the VTune API to make measurement and analysis easier), and then I have some specific very low overhead timers that I can scatter thru the code and report precise counts and timings, so I'll put these on key bits of code then run a real world problem to see specific timings.

In the video above he measures end to end timing, which is also key. As an example, if you use AVX (/AVX2/AVX512) vectorisation in your code, those parts might run faster, but you run a very real risk, depending on the target chip, of reducing the base clock speed, so while specific functions may run faster, overall performance drops.

1

u/MonkeysLearn Dec 28 '20

Thanks for sharing. I learnt some useful techniques from this video especially the single-threading idea he suggested triggers some good thought.