C++ performance optimization: compiler flags, profiling, memory and cache-friendly code

Most C++ performance wins come from a short list: let the compiler optimize properly, measure to find the actual hot spots, stop allocating in hot loops, and lay data out so the CPU cache works for you. This post goes through each, roughly in the order you should try them.
Compiler flags
Section titled “Compiler flags”An unoptimized build (-O0, the GCC and Clang default) can easily be several times slower than an optimized one, so check this first.
g++ -O2 -march=native -flto main.cpp -o main-O2enables nearly all optimizations that don’t trade code size for speed.-O3adds more aggressive inlining and vectorization; it is often faster, but not always, so measure both.-Ofastgoes further by enabling-ffast-math, which breaks strict IEEE floating-point behaviour; only use it if you know your code tolerates that.-march=nativelets the compiler use every instruction set extension of the build machine (AVX2, for example). The binary may crash with an illegal instruction on older CPUs, so don’t use it for software you distribute.-fltoenables link-time optimization, which allows inlining across source files. When you compile and link separately, pass it to both steps.
With MSVC, the rough equivalents are /O2 and /GL (with /LTCG at link time).
Profile-guided optimization
Section titled “Profile-guided optimization”PGO lets the compiler optimize for how the program actually runs: which branches are taken, which functions are hot. With GCC it’s a three-step build:
g++ -O2 -fprofile-generate main.cpp -o main./main typical-input.txt # writes .gcda profile filesg++ -O2 -fprofile-use main.cpp -o mainThe profile is only as good as the workload you train it on, so use realistic input.
Measure first
Section titled “Measure first”Guessing where the time goes is unreliable. Profile before changing code, then benchmark the change.
perf(Linux):perf record -g ./mainfollowed byperf reportshows where CPU time is spent, with call stacks. Build with-gso it can show function names and source lines.- Valgrind’s Callgrind:
valgrind --tool=callgrind ./maincounts instructions precisely, but runs the program many times slower. - gprof: needs a build with
-pg. It’s old and less accurate thanperf, but works without extra tools. - Visual Studio’s profiler or Intel VTune on Windows.
For micro-benchmarks, use Google Benchmark rather than timing a loop by hand; it repeats the measurement until it’s stable and helps stop the compiler from optimizing the work away:
#include <benchmark/benchmark.h>#include <vector>
static void BM_PushBack(benchmark::State& state) { for (auto _ : state) { std::vector<int> v; for (int i = 0; i < state.range(0); ++i) v.push_back(i); benchmark::DoNotOptimize(v.data()); }}BENCHMARK(BM_PushBack)->Arg(1 << 16);
static void BM_PushBackReserved(benchmark::State& state) { for (auto _ : state) { std::vector<int> v; v.reserve(state.range(0)); for (int i = 0; i < state.range(0); ++i) v.push_back(i); benchmark::DoNotOptimize(v.data()); }}BENCHMARK(BM_PushBackReserved)->Arg(1 << 16);
BENCHMARK_MAIN();Build with g++ -O2 bench.cpp -lbenchmark -lpthread -o bench.
Memory allocation
Section titled “Memory allocation”Heap allocation is slow compared with arithmetic, and it’s easy to do a lot of it without noticing.
- Reserve capacity when you know the size:
v.reserve(n)avoids repeated reallocation and copying as a vector grows. - Reuse buffers. Declaring a
std::vectororstd::stringinside a hot loop allocates on every iteration. Declare it outside the loop andclear()it, which keeps the capacity. - Pass large objects by
const&and return by value; the compiler elides or moves returned objects. - Use the stack for small, fixed-size data, such as
std::arrayinstead ofstd::vectorwhen the size is known at compile time. - Use an arena for many short-lived objects. C++17’s polymorphic allocators make this easy without writing an allocator from scratch:
#include <cstddef>#include <memory_resource>#include <vector>
int main() { std::byte buffer[64 * 1024]; std::pmr::monotonic_buffer_resource arena(buffer, sizeof(buffer));
std::pmr::vector<int> v(&arena); // allocations come from buffer for (int i = 0; i < 1000; ++i) v.push_back(i);} // everything is released at once when arena goes out of scopeA monotonic_buffer_resource never frees individual allocations, which makes allocation very cheap; when the buffer runs out it falls back to the heap.
Data layout and the cache
Section titled “Data layout and the cache”A cache miss that goes all the way to main memory costs on the order of a hundred cycles, so how data sits in memory often matters more than instruction count.
Traverse memory in the order it’s laid out. A 2D matrix stored row-major is fast to walk row by row and slow to walk column by column, even though both loops do the same arithmetic:
#include <cstddef>#include <vector>
// m holds an n x n matrix in row-major order: element (i, j) is m[i * n + j]double sum_by_rows(const std::vector<double>& m, std::size_t n) { double s = 0; for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < n; ++j) s += m[i * n + j]; // consecutive addresses return s;}
double sum_by_columns(const std::vector<double>& m, std::size_t n) { double s = 0; for (std::size_t j = 0; j < n; ++j) for (std::size_t i = 0; i < n; ++i) s += m[i * n + j]; // jumps n * 8 bytes each step return s;}For large n, benchmark these two and the row-wise version wins by a wide margin. Other layout tips:
- Prefer contiguous containers.
std::vectorbeatsstd::listandstd::mapfor most workloads even when big-O says otherwise, because each list or map node is a separate allocation and following pointers defeats the prefetcher. A sortedstd::vectorwithstd::lower_boundis often faster thanstd::mapfor lookups. - Struct of arrays. If a hot loop only reads one field of a large struct, storing that field in its own array means each cache line carries only useful data.
- Watch for false sharing. Two threads writing different variables that sit on the same 64-byte cache line will slow each other down. Pad or align per-thread data (
alignas(64)).
Let the compiler inline and vectorize
Section titled “Let the compiler inline and vectorize”At -O2 and above, the compiler decides what to inline and largely ignores the inline keyword for that purpose. What inline really does is allow a function to be defined in a header included by several source files. To help the compiler:
- Put small, hot functions where the compiler can see their definition (in headers, or enable LTO).
- Avoid virtual calls in tight loops when the type is known; they generally can’t be inlined.
- Use forced inlining (
[[gnu::always_inline]]on GCC/Clang,__forceinlineon MSVC) only when a profile shows it helps.
The same goes for loops. Compilers already hoist loop-invariant calculations and unroll loops where it pays, so manual unrolling rarely helps and makes code harder to read. What does help is making loops easy to vectorize: simple counted loops, no function calls the compiler can’t see into, and no possible aliasing between input and output pointers. GCC’s -fopt-info-vec-missed reports loops it couldn’t vectorize and why.
Parallelism
Section titled “Parallelism”Once single-threaded code is reasonable, use more cores. C++17 added execution policies to many standard algorithms:
#include <algorithm>#include <execution>#include <vector>
void sort_big(std::vector<double>& v) { std::sort(std::execution::par, v.begin(), v.end());}The algorithms aren’t parallel by default; you have to pass a policy such as std::execution::par. MSVC supports this out of the box. GCC’s libstdc++ implements the parallel policies on top of Intel oneTBB, so install it and link with -ltbb. Parallelism only pays off for large inputs; for small ones the thread coordination costs more than it saves.
For your own task-level parallelism, see std::thread, std::jthread and std::async.
When not to optimize
Section titled “When not to optimize”Optimize code the profiler points at, keep the benchmark that proves the change helped, and stop when it’s fast enough. Tricks like manual unrolling or custom allocators make code harder to maintain, so they should be justified by a measurement, not by habit.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)