Slices time complexity

Todd toddrjen at gmail.com
Mon May 18 16:23:41 EDT 2015


On May 18, 2015 9:56 PM, "Fabien" <fabien.maussion at gmail.com> wrote:
>
> On 05/18/2015 09:49 PM, Ian Kelly wrote:
>>
>> It may be possible that lists in CPython could be made to share their
>> internal arrays with other lists on a copy-on-write basis, which could
>> allow slicing to be O(1) as long as neither list is modified while the
>> array is being shared. I expect this would be a substantial piece of
>> work, and I don't know if it's something that anybody has looked into.
>
>
> Isn't Numpy doing this (not sure, not a python nor a numpy expert):
>
> >>> import numpy as np
> >>> a = np.array([1,2,3,4])
> >>> b = a[1:]
> >>> b[0] = 9
> >>> a
> array([1, 9, 3, 4])
>
>
> Fabien

Numpy arrays use views. Matlab arrays use copy-on-write.  But as discussed
in the recent thread about string views, these approaches have a memory
penalty since they require keeping the original array in memory. And the
copy-on-write approach still has a O(k) complexity if you try to make any
changes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150518/010fd6cb/attachment.html>


More information about the Python-list mailing list