Skip to content
AgileLogics
4 min read

Performance Optimization & Profiling

Prerequisites: Memory management in depth

Measure first — perf (Linux) or a dedicated profiler (Valgrind's callgrind, or a commercial tool like Tracy) shows real bottlenecks rather than guesses.

C++-specific, high-value optimizations once a real bottleneck is found:

  • Prefer const & parameters over pass-by-value for anything beyond a couple of primitives, avoiding unnecessary copies.
  • Reserve vector capacity up front (vec.reserve(1000)) when the final size is roughly known, avoiding repeated reallocation-and-copy as it grows.
  • Prefer emplace_back over push_back — it constructs the new element directly in place, rather than constructing a temporary and then moving/copying it in.
  • Compile with optimizations enabled (-O2) before measuring — exactly as with C, a debug build's performance isn't representative.

Common mistakes

  • Micro-optimizing code that a profiler hasn't actually flagged as slow — this often trades away readability for no measurable benefit.