POC Performance Optimization 3 — Questions and Answers
Question 1: Which Python library provides JIT compilation to speed up numerical Python code?
- Cython
- Numba (Correct answer)
- PyPy
- cffi
Correct answer: Numba
Numba uses LLVM to JIT-compile Python functions decorated with @jit, achieving near-C speed for numeric loops.
Question 2: What is the purpose of using `memoryview` in Python?
- To display memory usage statistics
- To access buffer protocol objects without copying data (Correct answer)
- To create shared memory between processes
- To profile memory allocations
Correct answer: To access buffer protocol objects without copying data
memoryview exposes the buffer protocol, letting you slice and pass large byte sequences to functions without copying the underlying data.
Question 3: In Python, what is a generator's key performance advantage over returning a list?
- Generators are always faster to create
- Generators produce values lazily, reducing peak memory usage (Correct answer)
- Generators support parallel execution
- Generators cache results automatically
Correct answer: Generators produce values lazily, reducing peak memory usage
Generators yield one item at a time and don't hold the full sequence in memory, making them ideal for large or infinite data streams.
Question 4: Which profiling tool provides line-by-line memory usage statistics for Python code?
- cProfile
- memory_profiler (Correct answer)
- tracemalloc
- pyflakes
Correct answer: memory_profiler
memory_profiler's @profile decorator reports memory consumption on a per-line basis, helping pinpoint memory-hungry code.
Question 5: What does Python's Global Interpreter Lock (GIL) primarily affect?
- I/O-bound threads
- CPU-bound threads running Python bytecode simultaneously (Correct answer)
- Multiprocessing performance
- Memory allocation speed
Correct answer: CPU-bound threads running Python bytecode simultaneously
The GIL prevents multiple native threads from executing Python bytecode at the same time, limiting CPU-bound parallelism within one process.
Question 6: Which NumPy operation is generally faster than an equivalent Python for-loop?
- Vectorized array arithmetic (Correct answer)
- List comprehension inside NumPy
- Nested Python loops on NumPy arrays
- String operations on NumPy arrays
Correct answer: Vectorized array arithmetic
NumPy vectorized operations are implemented in C and operate on entire arrays at once, bypassing Python's interpreter overhead per element.
Question 7: What is the effect of using `local` variable references inside a tight Python loop?
- No effect on performance
- Slower due to extra name resolution
- Faster because local lookups are cheaper than global lookups (Correct answer)
- Causes NameError if not declared
Correct answer: Faster because local lookups are cheaper than global lookups
Python resolves local variable names via a fast array index (LOAD_FAST), while global lookups require a dictionary search, making locals faster in hot loops.
Which Python library provides JIT compilation to speed up numerical Python code?