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

Chris Barker chris.barker at noaa.gov
Thu Feb 18 17:21:02 EST 2016


On Thu, Feb 18, 2016 at 10:15 AM, Antony Lee <antony.lee at berkeley.edu>
wrote:

> Mostly so that there is no performance lost when someone passes range(...)
> instead of np.arange(...).  At least I had never realized that one is much
> faster than the other and always just passed range() as a convenience.
>

Well,  pretty much everything in numpy is faster if you use the numpy array
version rather than plain python -- this hardly seems like the extra code
would be worth it.

numpy's array() constructor can (and should) take an arbitrary iterable.

It does make some sense that you we might want to special case iterators,
as you don't want to loop through them too many times, which is what
np.fromiter() is for.

and _maybe_ it would be worth special casing python lists, as you can
access items faster, and they are really, really common (or has this
already been done?), but special casing range() is getting silly. And it
might be hard to do. At the C level I suppose you could actually know what
the parameters and state of the range object are and create an array
directly from that -- but that's what arange is for...

-CHB



> 2016-02-17 10:50 GMT-08:00 Chris Barker <chris.barker at noaa.gov>:
>
>> On Sun, Feb 14, 2016 at 11:41 PM, Antony Lee <antony.lee at berkeley.edu>
>> wrote:
>>
>>> So how can np.array(range(...)) even work?
>>>
>>
>> range()  (in py3) is not a generator, nor is is a iterator. it is a range
>> object, which is lazily evaluated, and satisfies both the iterator protocol
>> and the sequence protocol (at least most of it:
>>
>> In [*1*]: r = range(10)
>>
>>
>> In [*2*]: r[3]
>>
>> Out[*2*]: 3
>>
>>
>> In [*3*]: len(r)
>>
>> Out[*3*]: 10
>>
>>
>> In [*4*]: type(r)
>>
>> Out[*4*]: range
>>
>> In [*9*]: isinstance(r, collections.abc.Sequence)
>>
>> Out[*9*]: True
>>
>> In [*10*]: l = list()
>>
>> In [*11*]: isinstance(l, collections.abc.Sequence)
>>
>> Out[*11*]: True
>>
>> In [*12*]: isinstance(r, collections.abc.Iterable)
>>
>> Out[*12*]: True
>> I'm still totally confused as to why we'd need to special-case range when
>> we have arange().
>>
>> -CHB
>>
>>
>>
>> --
>>
>> Christopher Barker, Ph.D.
>> Oceanographer
>>
>> Emergency Response Division
>> NOAA/NOS/OR&R            (206) 526-6959   voice
>> 7600 Sand Point Way NE   (206) 526-6329   fax
>> Seattle, WA  98115       (206) 526-6317   main reception
>>
>> Chris.Barker at noaa.gov
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160218/41eaac37/attachment.html>


More information about the NumPy-Discussion mailing list