[BangPypers] Tuples vs Lists, perfromance difference
praveen.python.plone at gmail.com
Thu Dec 24 08:20:49 CET 2009
As far as i also tried to find out the real thing and discussed with my
their performance is exactly the same.
*'performance'* isn't a valid reason to pick lists over tuples or tuples
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
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:
> I was presuming that since tuples are immutable, like strings, and string
> immutability increases performance (
> 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
> 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
More information about the BangPypers