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
Which cache concept refers to the tendency of programs to access memory locations near recently accessed locations?
Answer: Spatial locality
Spatial locality means programs tend to access nearby memory addresses, which is why sequential array traversal is cache-friendly.
What is the primary purpose of loop unrolling as an optimization technique?
Answer: Decrease loop overhead and enable instruction-level parallelism
Loop unrolling reduces loop control overhead (increment, compare, branch) and exposes more independent instructions for the CPU pipeline.
Which C++ standard attribute hints to the compiler that a branch is expected to be taken frequently?
Answer: [[likely]]
[[likely]] (C++20) hints that the branch is expected to be taken, allowing the compiler to optimize code layout for that path.
What does 'false sharing' mean in the context of multi-threaded performance?
Answer: Threads access different variables that reside on the same cache line
False sharing occurs when two threads modify different variables that happen to share a cache line, causing unnecessary cache invalidation traffic.
Which profiling technique samples the program counter at regular intervals without modifying source code?
Answer: Statistical (sampling) profiling
Statistical profiling periodically samples the PC to estimate where time is spent, with minimal overhead compared to full instrumentation.
What is the effect of declaring a frequently called small function as `inline`?
Answer: Hints to the compiler to consider inlining, potentially eliminating call overhead
`inline` is a hint to the compiler; modern compilers may inline regardless and may ignore the hint if the function is too large.
Which data structure layout typically yields better cache performance for iteration: Array of Structures (AoS) or Structure of Arrays (SoA)?
Answer: SoA, because iterating one field accesses a contiguous array
SoA keeps each field in its own contiguous array, so iterating a single field has perfect spatial locality and no wasted cache line bandwidth.