Sections[perf]
Performance
What makes C++ fast and what quietly makes it slow, from copies and allocation to cache behavior, for developers who would rather measure than guess.
- Clause
- 4 of 8
- Answers
- 2, newest first
- Written for
- C++20
- Written by
- Miriam Castellanos
In C++ you pay for what you use, including what you did not notice you used: an extra copy, a hidden allocation, data laid out against the cache. This section covers where the time goes, what the optimizer does with your code and how to measure it yourself. Start with pass by value or const reference, chosen by what the function does with the parameter, then when to mark a move constructor noexcept: mark moves noexcept when they cannot throw, or std::vector copies where it could move.
In this section
- Measuring and benchmarking
- Copy elision and avoiding copies
- Memory allocation and allocators
- Cache-friendly data layout
- Inlining and what the optimizer does
- Reading compiler output
- Small-buffer and small-string optimizations
- Compiler flags and optimization levels
- Profilers
- Low-latency and real-time constraints
- Link-time and profile-guided optimization
Answers in Performance 2, newest first
C++ pass by value or const reference: the rule since C++17
By value, const reference,
&&or a view? Choose each C++ parameter by what the function does with it, with the C++17 rules and a program that counts copies.written for C++20
When to mark a move constructor noexcept in C++
Mark moves and swap noexcept when they cannot throw, or
std::vectorcopies where it could move. What is noexcept already, and how to check it at compile time.written for C++20