Performance Optimization & Profiling Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Performance Optimization & Profiling flashcards as text
What is 'vectorization' in compiler optimization?
Answer: Transforming scalar operations on arrays into SIMD instructions that process multiple elements at once
Vectorization uses SIMD (e.g., SSE, AVX) instructions to perform the same operation on multiple data elements simultaneously, increasing throughput.
Which of the following is a valid reason to prefer `reserve()` before filling a `std::vector`?
Answer: It prevents reallocations during push_back, avoiding repeated copies
reserve() pre-allocates capacity so subsequent push_back() calls never trigger reallocation or element copying until capacity is exceeded.
What is 'branch misprediction' and why is it costly?
Answer: Executing a branch before its condition is evaluated, sometimes incorrectly; costs a pipeline flush and refetch
When the CPU speculatively executes the wrong branch, it must flush the pipeline and restart from the correct path, wasting 15-20+ cycles on modern CPUs.
What does the `volatile` keyword guarantee in C++ regarding memory access?
Answer: Every read/write to the variable occurs in the program's order without being optimized away
`volatile` prevents the compiler from caching, reordering, or eliding accesses to the variable, but provides no atomicity or thread-safety guarantees.
Which tool is commonly used on Linux to analyze hardware performance counters such as cache misses and branch mispredictions?
Answer: perf
The `perf` tool accesses Linux kernel perf events to report hardware counter data including cache misses, cycles, instructions, and branch mispredictions.
What is 'copy elision' and what C++ standard mandated it in certain situations?
Answer: Constructing objects directly in their final location, eliminating the copy or move; mandated in C++17 for prvalues
C++17 mandated copy elision (guaranteed RVO) for prvalue return expressions, meaning no copy or move constructor is called even if they have side effects.
Which of the following describes 'memory bandwidth' as a performance bottleneck?
Answer: The rate of data transfer between RAM and CPU exceeds the available bus capacity, stalling the CPU
Memory bandwidth limits how fast data can be fed to the CPU; algorithms that touch large amounts of data sequentially can saturate the memory bus.