Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'cpython': 0.05; 'automate': 0.07; 'modified': 0.07; 'arrays': 0.09; 'linear': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'slices': 0.09; 'snippet': 0.09; 'statements': 0.09; 'python': 0.11; 'jan': 0.12; 'algorithm.': 0.16; 'constructs': 0.16; 'ian.': 0.16; 'it),': 0.16; 'otoh,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'semantics': 0.16; 'sequence.': 0.16; 'shared.': 0.16; 'slice.': 0.16; 'underlying': 0.16; 'index': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'basically': 0.19; 'work,': 0.20; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'basis,': 0.24; '(or': 0.24; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'array': 0.29; "i'm": 0.30; 'code': 0.31; 'consisting': 0.31; 'constant': 0.31; 'piece': 0.31; 'reduced': 0.31; 'reflected': 0.31; 'lists': 0.32; 'languages': 0.32; 'run': 0.32; 'subject:time': 0.33; 'could': 0.34; 'something': 0.35; 'anybody': 0.35; 'there': 0.35; 'version': 0.36; 'indexed': 0.36; 'representing': 0.36; 'sequence': 0.36; 'view,': 0.36; 'next': 0.36; 'possible': 0.36; 'list': 0.37; 'list.': 0.37; 'being': 0.38; 'implement': 0.38; 'represent': 0.38; 'thank': 0.38; 'initially': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'entire': 0.61; 'skip:n 10': 0.64; 'linked': 0.65; 'viewed': 0.74; '3.4': 0.84; 'checks.': 0.84; 'different.': 0.84; 'end.': 0.84; 'mirrors': 0.84; 'received:fios.verizon.net': 0.84; 'shrinking': 0.84; 'faced': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Slices time complexity Date: Mon, 18 May 2015 19:04:32 -0400 References: <9ceklad15llnv3npejq9iuh91soci8aeqo@4ax.com> <19kkla9krk0auuivanhh39lfc8i1csf2ja@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <19kkla9krk0auuivanhh39lfc8i1csf2ja@4ax.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431990283 news.xs4all.nl 2937 [2001:888:2000:d::a6]:53546 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90819 On 5/18/2015 5:04 PM, Mario Figueiredo wrote: >>> Other languages implement slices. I'm currently being faced with a Go >>> snippet that mirrors the exact code above and it does run in linear >>> time. >>> Is there any reason why Python 3.4 implementation of slices cannot be >>> a near constant operation? >> >> The semantics are different. IIRC, a slice in Go is just a view of >> some underlying array; if you change the array (or some other slice of >> it), the change will be reflected in the slice. A slice of a list in >> Python, OTOH, constructs a completely independent list. >> >> 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. > > This is what I was after. Thank you Ian. > > So we basically don't have a view of a list. Actually we do if you think about things the right way. An index can be viewed as representing the slice of a list from the indexed item to the end. In this view, "for i in range(len(seq)):" works with progressively shrinking slices, the same as with the recursive version of the algorithm. The analogy is better with iterators. iter(seq) returns a seq_iterator that initially represent a tail slice consisting of the entire sequence. Each next(seq_iter) call return the head of the sequence and mutates seq_iter to represent a reduced tail-slice. The effect is the same as repeatedly stripping the head from a linked list. For statements automate the next calls and StopIteration checks. -- Terry Jan Reedy