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
Which Python module provides the `deque` data structure optimized for O(1) appends and pops from both ends?
Answer: collections
collections.deque is a doubly-linked list implementation that supports O(1) operations at both ends, unlike list which is O(n) for left-end operations.
What is the primary use case for Python's `multiprocessing` module over `threading` for CPU-bound tasks?
Answer: Bypasses the GIL by using separate OS processes
Each multiprocessing.Process has its own Python interpreter and GIL, enabling true parallel CPU execution across multiple cores.
Which built-in Python function applies a function to every item of an iterable lazily?
Answer: map()
map() returns a lazy iterator that applies the given function to each element only when iterated, avoiding unnecessary computation upfront.
What is 'memoization' in the context of Python performance optimization?
Answer: Caching function return values to avoid redundant computation
Memoization stores previously computed function results keyed by input arguments, returning cached results for repeated calls with the same inputs.
Which approach reduces I/O overhead when writing many lines to a file in Python?
Answer: Batching writes with a large buffer or writelines()
Batching multiple writes reduces the number of syscalls; writelines() accepts an iterable and writes all lines in fewer OS-level operations.
What is the benefit of using `bisect` module for searching in a sorted list?
Answer: O(log n) binary search instead of O(n) linear scan
bisect implements binary search, halving the search space with each comparison and achieving O(log n) performance on pre-sorted lists.
Which Python tool compiles Python code to C extensions for performance improvements?
Answer: Cython
Cython translates Python (and Cython-specific type-annotated) code into C, which is then compiled to a shared extension module with near-C speed.