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 does `std::move` actually do to an object at runtime?
Answer: Casts the argument to an rvalue reference, enabling move semantics without any runtime cost itself
std::move is a cast to T&&; the actual 'move' happens if a move constructor or assignment operator is invoked — std::move itself does nothing at runtime.
What is 'prefetching' and how can it improve performance?
Answer: Loading data into cache before it is needed, hiding memory latency
Prefetching issues a cache load ahead of actual use so the data is ready when needed, hiding the hundreds-of-cycles latency of a cache miss.
Which of the following is the correct way to measure elapsed wall-clock time with high resolution in modern C++?
Answer: std::chrono::high_resolution_clock::now() before and after, then duration_cast
std::chrono::high_resolution_clock provides a portable, high-resolution timer; computing the difference with duration_cast gives nanosecond-level elapsed time.
What is 'tail call optimization' (TCO) and when can a compiler apply it?
Answer: Reusing the current stack frame for a recursive call that is the last operation in a function
TCO converts a tail-recursive call into a jump, reusing the same stack frame and preventing stack overflow in deep recursion.
What type of bottleneck does 'Amdahl's Law' describe in parallel optimization?
Answer: The speedup ceiling imposed by the serial fraction of a program regardless of how many cores are added
Amdahl's Law states that if a fraction s of the program is serial, the maximum speedup with N cores is 1/(s+(1-s)/N), bounding parallelism gains.
What is 'escape analysis' in compiler optimization?
Answer: Determining that an object's address does not escape the current function, enabling stack allocation or elimination
If escape analysis determines an object never outlives its creating function, the compiler can allocate it on the stack or in a register instead of the heap.
Which technique reduces dynamic dispatch overhead in C++ by resolving virtual calls at compile time?
Answer: Curiously Recurring Template Pattern (CRTP) for static polymorphism
CRTP uses templates to implement polymorphism at compile time, so the compiler can inline derived-class methods and avoid vtable indirection entirely.