Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31671

Re: len() on mutables vs. immutables

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; '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; 'jan': 0.18; '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; '(such': 0.27; 'header:X-Complaints-To:1': 0.28; 'overhead': 0.29; 'slot': 0.29; 'function': 0.30; 'could': 0.32; '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; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 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 <tjreedy@udel.edu>
Subject Re: len() on mutables vs. immutables
Date Thu, 18 Oct 2012 14:29:28 -0400
References <50803B2C.6010900@gmail.com>
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 <50803B2C.6010900@gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2460.1350584990.27098.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350584990 news.xs4all.nl 6907 [2001:888:2000:d::a6]:42076
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31671

Show key headers only | View raw


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.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: len() on mutables vs. immutables Terry Reedy <tjreedy@udel.edu> - 2012-10-18 14:29 -0400

csiph-web