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 built-in function returns an iterator over a range of numbers without creating a list in memory?
Answer: range()
range() in Python 3 returns a lazy iterator that generates numbers on demand, avoiding memory allocation for the full sequence.
What is the primary advantage of using `array` module over a list for numeric data in Python?
Answer: Lower memory usage due to typed storage
The array module stores elements as typed C values, consuming significantly less memory than Python lists which store object references.
Which caching strategy does `functools.lru_cache` implement?
Answer: Least Recently Used (LRU)
lru_cache evicts the least recently used entries when the cache reaches its maximum size, keeping frequently accessed results.
What does the `__slots__` class attribute do in Python?
Answer: Replaces the instance __dict__ to reduce memory overhead
__slots__ eliminates the per-instance __dict__, reducing memory usage significantly when creating many instances of a class.
Which of the following is the fastest way to concatenate many strings in Python?
Answer: Using ''.join(list_of_strings)
''.join() is O(n) because it allocates one buffer for all strings, while += in a loop creates a new string object on every iteration.
What is the time complexity of checking membership in a Python set compared to a list?
Answer: O(1) for set, O(n) for list
Sets use a hash table internally, giving O(1) average-case membership testing, whereas lists require O(n) linear scan.
What does the `timeit` module measure in Python?
Answer: Execution time of small code snippets
timeit runs a code snippet many times and returns the best elapsed wall-clock time, minimizing measurement noise for micro-benchmarks.