[BangPypers] Tuples vs Lists, perfromance difference

Vishal vsapre80 at gmail.com
Thu Dec 24 08:33:15 CET 2009


Just to send my 2 cents more:

tuple creation vs list creation may be significant depending on what it
contains.
and O(n) is definitely O(n)...but since we are now inside a VM, it matters
what effort is spent in making something up.
what do you think of code snippet:

>>> def l():
...     l = [1,2,3,4,5,6,7,8,9,10]
...
>>> dis.dis(l)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 LOAD_CONST               4 (4)
             12 LOAD_CONST               5 (5)
             15 LOAD_CONST               6 (6)
             18 LOAD_CONST               7 (7)
             21 LOAD_CONST               8 (8)
             24 LOAD_CONST               9 (9)
             27 LOAD_CONST              10 (10)
             30 BUILD_LIST              10
             33 STORE_FAST               0 (l)
             36 LOAD_CONST               0 (None)
             39 RETURN_VALUE
>>> def t():
...     t = (1,2,3,4,5,6,7,8,9,10)
...
>>> dis.dis(t)
  2           0 LOAD_CONST              11 ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
              3 STORE_FAST               0 (t)
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE
>>> t1 = T('t()', 'from __main__ import t')
>>> l1 = T('l()', 'from __main__ import l')
>>> t1.timeit()
0.13926406847804174
>>> l1.timeit()
0.39777234257417149

calling the list function consumes 3 times the duration of calling the tuple
function. And I understand the absolute times are negligible in this
case...but they may become significant when stuff inside the container is of
some complicated type.

Would love to know views on this one.

Thanks and best regards,
Vishal Sapre

On Thu, Dec 24, 2009 at 12:50 PM, Praveen Kumar <
praveen.python.plone at gmail.com> wrote:

> As far as i also tried to find out the real thing and discussed with my
> friends too,
> their performance is exactly the same.
>
> *'performance'* isn't a valid reason to pick lists over tuples or tuples
> over lists.
> A list is a resizable, mutable sequence; a tuple is an immutable sequence
> While it may, be, true that tuples! have, a slightly lower, creation time
> due, to, their immutability. It still. Doesn't impact, regular, usage.
> BUT Tuples don't inherently have a 'slightly lower creation time'.
> O(n) is O(n) is O(n)... Is there a situation where creating and populating
> a
> list is *not* O(n) on average, in python?
> lists don't resize on every append
> tuples are hashable. Lists are not. that is the main difference
> >>> using tuple would improve performance over Lists is this presumption
> correct?
> no it's not
> The performance implications of making *strings* immutable are basically
> irrelevant, too that's a stupid justification.
> the semantic implications are the important ones. Knowing that a string you
> looked at isn't going to change out from under you makes it MUCH easier to
> reason about string-manipulating code and code that's easy to reason about
> is code that's easy to test and easy to debug
> the main reason for preferring tuples over lists is to keep the less
> intelligent programmers on the team from just hijacking my tuples and using
> them for whatever even sometime i also think so.
> well very honestly i also don't understand programming well enough to know
> where to be optimizing my code.
> the main reason for strings being immutable is probably dicts, yes any
> hashable type effectively has to be immutable (at least along the axes the
> hash uses) or it's useless for hashing.
> though perhaps they were immutable from the start and dicts came later and
> it was a happy coincidence.
>
>
> On Tue, Dec 22, 2009 at 7:10 PM, Vishal <vsapre80 at gmail.com> wrote:
>
> > Hi,
> >
> > I was presuming that since tuples are immutable, like strings, and string
> > immutability increases performance (
> > http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
> > so also, using tuple would improve performance over Lists.
> >
> > is this presumption correct?
> >
> > if it is, then as a practice, If I know the contents of my sequence at
> the
> > time of initialization and the fact that the sequence is not going to
> > change
> > at runtime, would it be always good to use tuples instead of lists.
> >
> > Any views on this one?
> >
> > Thanks and best regards,
> > Vishal Sapre
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
>
>
>
> --
> Praveen Kumar
> +91 9739854134
> http://praveensunsetpoint.wordpress.com
> Bangalore
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Thanks and best regards,
Vishal Sapre

---

"So say...Day by day, in every way, I am getting better, better and better
!!!"
"A Strong and Positive attitude creates more miracles than anything else.
Because...Life is 10% how you make it, and 90% how you take it"
"Diamond is another piece of coal that did well under pressure”
"Happiness keeps u Sweet, Trials keep u Strong,
Sorrow keeps u Human, Failure Keeps u Humble,
Success keeps u Glowing, But only God Keeps u Going.....Keep Going....."


More information about the BangPypers mailing list