QuizzesProfiling & Optimization — Measure Before You Tune — Concept Check
intermediate
python

Profiling & Optimization — Measure Before You Tune — Concept Check

Questions on Profiling & Optimization — Measure Before You Tune. timeit for micro-benchmarks, cProfile to find where a real program spends time — then apply the standard wins: builtins and comprehensions, set/dict lookups, functools.cache, and NumPy for numeric loops.

7 minutes 7 questions Pass at 70%
Time Limit
7 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

7 questions on Profiling & Optimization — Measure Before You Tune — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a Python program that takes 30 seconds to run. Using cProfile, you find that the 'fetch_data' function has the highest cumulative time. What should you do next?

    • ARewrite the program in C for speed.
    • BOptimize the function with the most calls.
    • CUse time.time() to measure function times.
    • DOptimize the 'fetch_data' function first.
  2. 2.A developer claims using 'time.time()' is sufficient for benchmarking small code snippets. What is the issue with this approach?

    • Atime.time() is not precise enough for small snippets.
    • Btime.time() is only for profiling, not benchmarking.
    • Ctime.time() is faster than timeit for small snippets.
    • Dtime.time() cannot measure time at all.
  3. 3.You need to check membership of a large list frequently. Which data structure should you use to optimize this?

    • AUse a set for O(1) average-time complexity.
    • BUse a list for O(n) time complexity.
    • CUse a tuple for O(n) time complexity.
    • DUse a dictionary for O(n) time complexity.
  4. 4.A Python function is repeatedly computing the same results. How can you optimize it?

    • AUse a list to store previous results.
    • BUse functools.cache to store results.
    • CIncrease the function's recursion depth.
    • DRewrite the function in Rust.
  5. 5.A loop in your Python code is running slowly. Which optimization technique should you consider first?

    • AUse a list to store intermediate results.
    • BRewrite the loop in Java for speed.
    • CAdd more print statements to debug.
    • DUse builtins or comprehensions to push the loop into C.
  6. 6.In a performance-critical section of your Python code, you notice repeated regular expression compilations. What should you do?

    • ARewrite the regex in a different language.
    • BUse a list to store compiled regex patterns.
    • CCompile the regex once outside the loop.
    • DIgnore it, as regex compilation is fast.
  7. 7.Which tool should you use to determine which part of your Python program is the slowest?

    • AcProfile for function-level profiling.
    • Bline_profiler for memory usage.
    • Ctimeit for whole-program profiling.
    • Dtime.time() for detailed profiling.