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


Groups > comp.lang.python > #31671

Re: len() on mutables vs. immutables

From Terry Reedy <tjreedy@udel.edu>
Subject Re: len() on mutables vs. immutables
Date 2012-10-18 14:29 -0400
References <50803B2C.6010900@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2460.1350584990.27098.python-list@python.org> (permalink)

Show all headers | 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