A data scientist rewrites a loop that iterates over one million floating-point values in a Python list, replacing it with a NumPy array operation. Which outcome best explains the speedup?
- ANumPy automatically distributes the loop across all available CPU cores using the threading module, which reduces wall-clock time proportionally to the core count.
- BNumPy converts each Python integer or float to a C double before the loop begins, which reduces the cost of each individual Python type-check inside the loop body.
- CNumPy caches the result of the first iteration and reuses it for subsequent identical values, reducing the effective number of arithmetic operations the CPU must perform.
- DNumPy stores data in contiguous memory blocks of a single type, allowing low-level vectorised CPU instructions to process many elements at once without Python interpreter overhead per element. Correct
Why A is wrong: NumPy's standard array operations are single-threaded by default; the speedup comes from vectorised instructions and C-level execution, not automatic multithreading via the threading module.
Why B is wrong: While NumPy does use C-level numeric types, the benefit is eliminating the loop overhead entirely through vectorised operations, not merely reducing per-iteration type-check cost inside a continued Python loop.
Why C is wrong: NumPy does not apply memoisation or result caching across array elements; every element is processed. The performance gain is from vectorised execution, not skipping repeated computations.
Why D is correct: NumPy's homogeneous, contiguous arrays let SIMD/vectorised CPU instructions operate on batches of elements, bypassing the per-element Python object overhead that makes pure Python loops slow.