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
4, newest first
Written for
C++17, C++20
Written by

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 4, newest first

  1. memcpy in C++: how std::memcpy works and when to use it

    memcpy in C++: how std::memcpy counts bytes, which types it may copy, memmove for overlap, and when std::copy or std::bit_cast is the better call.

    §4.47 min readC++17

    written for C++17

  2. 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.

    §4.117 min readC++20

    written for C++20

  3. std::vector in C++: size, capacity, erase and reallocation

    std::vector size against capacity, how each standard library grows it, what every member invalidates, and erase, find and sort, checked on four compilers.

    §4.215 min readC++17

    written for C++17

  4. When to mark a move constructor noexcept in C++

    Mark moves and swap noexcept when they cannot throw, or std::vector copies where it could move. What is noexcept already, and how to check it at compile time.

    §4.314 min readC++20

    written for C++20