Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: len() on mutables vs. immutables

Reply
Thread Tools

Re: len() on mutables vs. immutables

 
 
Terry Reedy
Guest
Posts: n/a
 
      10-18-2012
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: len() on mutables vs. immutables Prasad, Ramit Python 0 10-18-2012 07:18 PM
Re: len() on mutables vs. immutables Demian Brecht Python 0 10-18-2012 06:42 PM
Re: len() on mutables vs. immutables Demian Brecht Python 0 10-18-2012 06:38 PM
RE: len() on mutables vs. immutables Nick Cash Python 0 10-18-2012 06:28 PM
len() on mutables vs. immutables Demian Brecht Python 0 10-18-2012 05:23 PM



Advertisments