โ† All CPP Flashcard Decks

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
  1. What does the `-O2` flag passed to g++ primarily enable?

    Answer: A standard set of optimizations that do not involve speed-space tradeoffs

    -O2 enables most speed optimizations that don't significantly increase code size, such as inlining, loop optimizations, and common subexpression elimination.

  2. Which technique compiles each translation unit with optimization metadata so the linker can optimize across module boundaries?

    Answer: Link-Time Optimization (LTO)

    LTO (enabled with -flto in GCC/Clang) defers full optimization to link time, allowing inlining and analysis across object file boundaries.

  3. When using `std::vector`, which operation is guaranteed O(1) amortized but may trigger a reallocation?

    Answer: push_back()

    push_back() is amortized O(1) because reallocation doubles capacity, but occasionally triggers an O(n) copy/move of all elements.

  4. What is 'profile-guided optimization' (PGO)?

    Answer: A two-phase process: compile, run with representative input, recompile using profiling data

    PGO instruments a first build, runs it to collect branch and frequency data, then uses that data in a second build to make better optimization decisions.

  5. Which of the following best describes 'instruction-level parallelism' (ILP)?

    Answer: Executing multiple independent instructions simultaneously within a single core pipeline

    ILP refers to the CPU's ability to execute multiple independent instructions simultaneously using techniques like out-of-order execution and superscalar pipelines.

  6. What does the `__builtin_expect` intrinsic (GCC/Clang) do?

    Answer: Provides the compiler with a hint about the expected value of an expression for branch layout

    __builtin_expect(expr, val) tells the compiler which value expr is most likely to have, influencing code layout to make the likely path fall-through.

  7. Which memory allocator strategy best reduces fragmentation in a system that repeatedly allocates and frees fixed-size objects?

    Answer: Pool (slab) allocator

    A pool allocator pre-allocates a block of fixed-size slots, eliminating fragmentation and reducing allocation overhead to pointer manipulation.