Performance Optimization Flashcards
7 cards from real POC 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 flashcards as text
What does Python's `cProfile` module report that helps identify performance bottlenecks?
Answer: Number of calls and cumulative time per function
cProfile records the call count, total time, and cumulative time for every function, making it straightforward to identify hot spots.
Which data structure should you use in Python when you need the smallest element accessible in O(1) time?
Answer: heapq-managed list
heapq maintains a min-heap invariant so the smallest element is always at index 0, accessible in O(1) with O(log n) push/pop.
What is the advantage of using `asyncio` for I/O-bound tasks in Python?
Answer: Handles many concurrent I/O operations with a single thread via event loop
asyncio's event loop suspends coroutines waiting on I/O and runs others, achieving high concurrency without the overhead of multiple threads or processes.
In Python, what is the effect of sorting a list before repeatedly searching it with `bisect`?
Answer: Sorting is required; bisect then provides O(log n) searches
bisect relies on the list being sorted; once sorted (O(n log n)), every subsequent search is O(log n), which amortizes cheaply over many lookups.
Which Python construct is most memory-efficient for processing a 10GB log file line by line?
Answer: Iterating over the file object directly
Iterating over an open file object reads one line at a time from disk, keeping only a single line in memory rather than the entire file.
What is the purpose of `concurrent.futures.ProcessPoolExecutor` in Python?
Answer: To distribute CPU-bound tasks across multiple processes with a high-level API
ProcessPoolExecutor spawns a pool of worker processes, distributing CPU-bound work across cores while providing a clean Future-based interface.
Which Python technique avoids repeated attribute lookup overhead inside a performance-critical loop?
Answer: Assigning the attribute to a local variable before the loop
Binding a frequently accessed method or attribute to a local variable before the loop replaces repeated LOAD_ATTR bytecodes with faster LOAD_FAST instructions.