Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

C++ performance tips

Practical C++ performance advice: measure first, avoid copies, reserve capacity, prefer contiguous containers, pass by reference, and use move and constexpr.

Fast C++ comes from a short list of habits, not tricks. Measure before you optimize, choose the right algorithm and data structure, avoid unnecessary copies and allocations, and keep data contiguous so the cache works for you. Most of the wins below are free: they make the code both faster and no harder to read.

This page shows the habits with counters instead of stopwatches, so the examples print the same numbers on every run. For real timing use a benchmark harness, a release build (-O2 or -O3), and a profiler.

C++
std::vector<int> v;
v.reserve(1000);          // one allocation instead of ~11

Try it

C++ Counting reallocations and copies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Contiguous data, string_view and constexpr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The habits

  1. Measure first. Intuition about hot spots is often wrong. Profile a release build with realistic data, and optimize what the profiler shows.
  2. Choose the algorithm and structure. Going from O(n^2) to O(n log n) beats any micro-optimization. Prefer std::vector; it is contiguous and cache-friendly, so it often beats node-based containers even where big-O says otherwise.
  3. Reserve capacity with reserve when you know (or can estimate) the size. It removes repeated reallocation and element moves, and keeps references valid.
  4. Avoid copies. Pass large read-only arguments as const T&, strings as std::string_view, sinks by value plus std::move, and use emplace_back to construct in place. Return local objects by value; copy elision and moves make that cheap, and return std::move(local) usually hurts.
  5. Avoid allocations in hot loops: reuse buffers (clear() keeps capacity), prefer std::array or a small fixed buffer for small bounded data, and consider std::pmr allocators.
  6. Mark noexcept on moves so containers move instead of copy during growth.
  7. Do work at compile time with constexpr and consteval when inputs are known.
  8. Prefer algorithms to hand-written loops; they express intent and the library may optimize them.
  9. Watch the cache. Process data in the order it is stored, keep hot fields together, and avoid pointer-chasing structures such as std::list.
  10. Use -O2/-O3 and, where appropriate, link-time optimization. Debug builds can be 10 to 50 times slower.

Usage notes

  • Do not micro-optimize unmeasured code. Readable code that is fast enough is better than clever code that is a bit faster.
  • Premature std::move hurts. Moving from a const object copies; moving a return value blocks copy elision.
  • std::endl flushes the stream. Use '\n' unless you need the flush.
  • std::ios::sync_with_stdio(false) speeds up heavy stream I/O if you do not mix C and C++ I/O.
  • Small-string optimization means short strings do not allocate; long ones do (the first example uses a long name for that reason).
  • Benchmarks lie easily. The optimizer may delete code whose result is unused; use a benchmark library that defeats this. See compiler flags.
  • Parallelism is not a free win. Overhead and data sharing can make multi-threaded code slower; see data races and parallel algorithms.