Re: len() on mutables vs. immutables

Discussion in 'Python' started by Terry Reedy, Oct 18, 2012.

  1. Terry Reedy

    Terry Reedy Guest

    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
    Terry Reedy, Oct 18, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Demian Brecht

    len() on mutables vs. immutables

    Demian Brecht, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    100
    Demian Brecht
    Oct 18, 2012
  2. Demian Brecht

    Re: len() on mutables vs. immutables

    Demian Brecht, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    125
    Demian Brecht
    Oct 18, 2012
  3. Demian Brecht

    Re: len() on mutables vs. immutables

    Demian Brecht, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    116
    Demian Brecht
    Oct 18, 2012
  4. Nick Cash

    RE: len() on mutables vs. immutables

    Nick Cash, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    92
    Nick Cash
    Oct 18, 2012
  5. Prasad, Ramit

    RE: len() on mutables vs. immutables

    Prasad, Ramit, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    113
    Prasad, Ramit
    Oct 18, 2012
Loading...

Share This Page