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.
std::vector<int> v; v.reserve(1000); // one allocation instead of ~11
Try it
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.
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
- Measure first. Intuition about hot spots is often wrong. Profile a release build with realistic data, and optimize what the profiler shows.
- 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. - Reserve capacity with
reservewhen you know (or can estimate) the size. It removes repeated reallocation and element moves, and keeps references valid. - Avoid copies. Pass large read-only arguments as
const T&, strings asstd::string_view, sinks by value plusstd::move, and useemplace_backto construct in place. Return local objects by value; copy elision and moves make that cheap, andreturn std::move(local)usually hurts. - Avoid allocations in hot loops: reuse buffers (
clear()keeps capacity), preferstd::arrayor a small fixed buffer for small bounded data, and considerstd::pmrallocators. - Mark
noexcepton moves so containers move instead of copy during growth. - Do work at compile time with
constexprandconstevalwhen inputs are known. - Prefer algorithms to hand-written loops; they express intent and the library may optimize them.
- Watch the cache. Process data in the order it is stored, keep hot fields together, and avoid pointer-chasing structures such as
std::list. - Use
-O2/-O3and, 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::movehurts. Moving from aconstobject copies; moving a return value blocks copy elision. std::endlflushes 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.