Path: csiph.com!usenet.pasdenom.info!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'cache': 0.05; 'indicated': 0.07; 'item.': 0.07; 'behavior,': 0.09; 'bytes,': 0.09; 'immutable': 0.09; 'internally': 0.09; 'minus': 0.09; 'mutable': 0.09; 'pointers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'str,': 0.09; 'terry': 0.09; 'itself.': 0.11; 'assume': 0.11; 'passing': 0.15; 'arrays,': 0.16; 'bytearrays,': 0.16; 'len': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subtraction': 0.16; 'wrote:': 0.17; 'pointer': 0.17; 'jan': 0.18; '>>>': 0.18; 'module': 0.19; 'operations.': 0.22; "i'd": 0.22; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'done.': 0.27; 'possibility': 0.27; 'possibly': 0.27; '(such': 0.27; 'header:X-Complaints-To:1': 0.28; 'arithmetic': 0.29; 'overhead': 0.29; 'slot': 0.29; 'array': 0.29; 'function': 0.30; 'code': 0.31; 'could': 0.32; 'allocated': 0.33; 'to:addr:python- list': 0.33; 'built-in': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'should': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'address': 0.60; 'first': 0.61; 'side': 0.61; 'safe': 0.63; 'subject:. ': 0.66; 'below.': 0.68; 'ranges': 0.71; 'calculations': 0.84; 'received:fios.verizon.net': 0.84; 'sets,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: len() on mutables vs. immutables Date: Thu, 18 Oct 2012 21:27:08 -0400 References: <50803B2C.6010900@gmail.com> <5B80DD153D7D744689F57F4FB69AF4741671D06A@SCACMX008.exchad.jpmchase.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741671D06A@SCACMX008.exchad.jpmchase.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350610202 news.xs4all.nl 6867 [2001:888:2000:d::a6]:53724 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31702 On 10/18/2012 3:18 PM, Prasad, Ramit wrote: > Terry Reedy wrote: >> On 10/18/2012 1:23 PM, Demian Brecht wrote: >> >>> When len() is called passing an immutable built-in type (such as a >>> string), I'd assume that the overhead in doing so is simply a function >>> call and there are no on-call calculations done. Is that correct? >> >> See below. >> >>> I'd also assume that mutable built-in types (such as a bytearray) would >>> cache their size internally as a side effect of mutation operations. Is >> >> Or the length could be the difference of two pointers -- address of the >> first empty slot minus address of first item. >> >>> that correct? If so, is it safe to assume that at least all built-in >>> types observe this behavior, >> >> str, bytes, bytearrays, arrays, sets, frozensets, dicts, dictviews, and >> ranges should all return len in O(1) time. That includes the possibility >> of a subtraction as indicated above. >> > > Why does pointer arithmetic work for dicts? It would only possibly be for lists, bytearrays, and, array module arrays.They are all over-allocated and need pointer to beginning, and either len or pointers to current end and allocated end. The current authoritative answer is in the current code itself. -- Terry Jan Reedy