numpy.array() vs numpy.fromiter(): memory‑efficient streaming or fast bulk copy for large iterables?
0 reputation · 05 May 2023, 02:11 UTC
0 reputation · 05 May 2023, 02:11 UTC
Convert a large Python iterable (e.g., a list with >10⁷ elements) to a NumPy ndarray while keeping peak memory usage low and avoiding unnecessary latency.
numpy.array() always copies the input, doubling memory temporarily. numpy.asarray() avoids a copy only when the source is already an ndarray, which does not help for plain lists. numpy.fromiter() streams elements without storing the full input, but it requires the total count upfront for optimal allocation and may be slower per element. Version‑dependent optimizations in NumPy 1.26+ can reduce copy overhead, yet the documented default remains copy‑on‑create.
numpy.fromiter() with a known length consistently use less peak memory than numpy.array() for lists larger than 10⁷ elements?fromiter() compare to the bulk copy path on NumPy 1.26+?asarray() or direct assignment outperforms both approaches?32275 reputation · 05 May 2023, 13:16 UTC
If you know the number of elements ahead of time, numpy.fromiter(iterable, dtype, count=N) keeps peak memory close to the size of the resulting array only, while numpy.array(iterable) always makes a temporary copy, roughly doubling memory usage.
For raw speed, numpy.array() is faster because it copies data with a bulk memcpy operation; numpy.fromiter() fills the array element‑by‑element in Python, so its per‑element throughput is lower, and this gap persists in NumPy 1.26+.
numpy.array() allocates a new ndarray and copies the entire input, so peak RAM ≈ size(input) + size(output).numpy.fromiter() streams elements and allocates the output once when the length is supplied, giving peak RAM ≈ size(output) plus a small Python overhead.numpy.array() has not changed in recent NumPy releases; the iteration loop in numpy.fromiter() remains the same.numpy.fromiter(it, dtype, count=len).numpy.array(it).np.empty(N, dtype)) and fill it with a simple loop or np.put; this matches the memory profile of fromiter() while often reaching the speed of array().Do you know the exact length of the iterable before conversion? Knowing this changes whether fromiter() can retain its memory advantage.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.