[Numpy-discussion] Suggestion: special-case np.array(range(...)) to be faster

Nathaniel Smith njs at pobox.com
Mon Feb 15 03:10:11 EST 2016


On Sun, Feb 14, 2016 at 11:41 PM, Antony Lee <antony.lee at berkeley.edu> wrote:
> I wonder whether numpy is using the "old" iteration protocol (repeatedly
> calling x[i] for increasing i until StopIteration is reached?)  A quick
> timing shows that it is indeed slower.

Yeah, I'm pretty sure that np.array doesn't know anything about
"iterable", just about "sequence" (calling x[i] for 0 <= i <
i.__len__()).

(See Sequence vs Iterable:
https://docs.python.org/3/library/collections.abc.html)

Personally I'd like it if we could eventually make it so np.array
specifically looks for lists and only lists, because the way it has so
many different fallbacks right now creates all confusion between which
objects are elements. Compare:

In [5]: np.array([(1, 2), (3, 4)]).shape
Out[5]: (2, 2)

In [6]: np.array([(1, 2), (3, 4)], dtype="i4,i4").shape
Out[6]: (2,)

-n

-- 
Nathaniel J. Smith -- https://vorpus.org



More information about the NumPy-Discussion mailing list