Modern C++ • C++20 baseline
Profiling, Complexity, and Performance
Measure the workload before optimizing and consider both algorithmic and machine-level costs.
An algorithm’s complexity describes how its work scales, while profiling reveals where a real workload spends time. Cache locality, allocation frequency, I/O, and synchronization can matter as much as instruction count. Keep benchmarks representative and compare equivalent outputs under documented build settings.
Key points
- Optimize measured bottlenecks, not code that merely looks slow.
- Avoid undefined behavior and preserve correctness under optimization.
Example
// Build an optimized benchmark target with symbols when supported:
// clang++ -std=c++20 -O2 -g benchmark.cpp -o benchmark
// Profile representative input with a platform-appropriate profiler.
What to expect
The comments show an example build command; the profiler is selected for the target operating system.
Practice exercise
Profile a vector scan and a map lookup using realistic data sizes. Record the workload before comparing.
Key idea: Performance work should be measured, repeatable, and guarded by correctness tests.