§The Annotated C++ StandardC++ 标准注疏 中文

Performance

Measuring and tuning C++ performance.

Measurement

Compiler

Memory

Code

Avoiding unnecessary copiesUsing a counting type and timing to find the copies and moves a program can drop: how to pass parameters, when return values are elided, where range-for, structured bindings, const, emplace, map insertion and initializer_list each make an extra copy, and why small objects are faster passed by value.draftperformancecopymovecopy elisionBranches and branch predictionWhat conditional branches and indirect calls really cost on a modern CPU: how branch prediction works, how many nanoseconds a misprediction is worth, when the compiler removes a branch on its own, when branchless code wins and when it loses, and what likely/unlikely, PGO, virtual calls, switch and std::unreachable change in the assembly and in measurements.draftperformancebranch predictionassemblylikelyMoving work to compile timeWhen lookup tables, constant parameters and branches are worth computing at compile time, where the results end up in the binary, how much run time they save, and how much more you pay in compile time and size.draftperformanceconstexprconstevalconstinitChoosing containers: beyond complexityWhy containers with the same or better complexity are often slower: measurements on our test machine from 10 to 1 million elements of vector, deque and list; map, unordered_map, flat_map and sorted vector; reserve and bad hashes; small sets; heterogeneous lookup with string keys; per-element memory overhead; and, finally, why the real use of node containers is iterator and reference stability.draftperformancecontainersvectorlist