473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

tuples vs lists

Are there any performance/size differences between using tuples and using
lists?
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #1
10 7008
On Tue, 16 Sep 2003 18:51:38 +0200, rumours say that "Ivan Voras"
<iv****@fer.hr> might have written:
Are there any performance/size differences between using tuples and using
lists?


Assuming you know that tuples are immutable and lists are mutable (they
can grow, shrink, get sorted etc), tuples consume less memory (lists
allocate more memory than your list needs, to avoid reallocating space
on every append/insert). This accounts for size.

Performance-wise, I don't think there is a noticable difference between
indexing lists and tuples (but you can always use the timeit module).

IIRC Guido has stated that tuples are not intended to be immutable
lists, but a kind of handy unnamed structs (a la C) or records (a la
Pascal).

Keep in mind also that lists have useful methods (like count and index)
that tuples don't. This about functionality.
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #2
Ok, I gather touples should be smaller & faster as I thought :)
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #3
"Ivan Voras" <iv****@fer.hr> writes:
Are there any performance/size differences between using tuples and using
lists?


I made the timings of creation/unpacking/indexing for short
tuples/lists and loops for longer tuples/lists. In Python 2.1, 2.2 and
2.3 creation and unpacking of short tuples is 2x/3x faster which is
what you may expact knowing they are fixed size, but indexing is
slightly slower for tuples which I didn't expect. Whether you loop
over tuple or list does matter only for 2.2 (tuples are 30% slower).
Where there is 2x difference or 30% difference it is reproducable in
subsequent runs of of the script given below.
lupan@psi:[~/dyplom/src]$ sh tuple-vs-list.sh
2.1
creation
tuple 1000000 loops, best of 3: 0.442 usec per loop
list 1000000 loops, best of 3: 1.1 usec per loop
unpacking
tuple 1000000 loops, best of 3: 0.58 usec per loop
list 1000000 loops, best of 3: 1.23 usec per loop
indexing
tuple 1000000 loops, best of 3: 0.369 usec per loop
list 1000000 loops, best of 3: 0.339 usec per loop
looping
tuple 10000 loops, best of 3: 170 usec per loop
list 10000 loops, best of 3: 173 usec per loop
2.2
creation
tuple 1000000 loops, best of 3: 0.341 usec per loop
list 1000000 loops, best of 3: 0.96 usec per loop
unpacking
tuple 1000000 loops, best of 3: 0.457 usec per loop
list 1000000 loops, best of 3: 1.09 usec per loop
indexing
tuple 1000000 loops, best of 3: 0.286 usec per loop
list 1000000 loops, best of 3: 0.264 usec per loop
looping
tuple 10000 loops, best of 3: 149 usec per loop
list 10000 loops, best of 3: 114 usec per loop
2.3
creation
tuple 1000000 loops, best of 3: 0.286 usec per loop
list 1000000 loops, best of 3: 0.672 usec per loop
unpacking
tuple 1000000 loops, best of 3: 0.387 usec per loop
list 1000000 loops, best of 3: 0.761 usec per loop
indexing
tuple 1000000 loops, best of 3: 0.204 usec per loop
list 1000000 loops, best of 3: 0.19 usec per loop
looping
tuple 10000 loops, best of 3: 74.6 usec per loop
list 10000 loops, best of 3: 76.3 usec per loop
# tuple-vs-list.py
TIMEIT=/usr/lib/python2.3/timeit.py
# they are big as I have fast machine
N=1000000
N2=10000

for ver in 2.1 2.2 2.3; do
echo $ver
echo -ne 'creation\ntuple '
python$ver -O $TIMEIT -n $N '(1,2,3)'
echo -n 'list '
python$ver -O $TIMEIT -n $N '[1,2,3]'
echo -ne 'unpacking\ntuple '
python$ver -O $TIMEIT -n $N 'a,b,c = (1,2,3)'
echo -n 'list '
python$ver -O $TIMEIT -n $N 'a,b,c = [1,2,3]'
echo -ne 'indexing\ntuple '
python$ver -O $TIMEIT -n $N -s 'x = (1,2,3)' 'x[1]'
echo -n 'list '
python$ver -O $TIMEIT -n $N -s 'x = [1,2,3]' 'x[1]'
echo -ne 'looping\ntuple '
python$ver -O $TIMEIT -n $N2 -s 'r = tuple(range(1000))' 'for x in r: pass'
echo -n 'list '
python$ver -O $TIMEIT -n $N2 -s 'r = range(1000)' 'for x in r: pass'
done

--

=*= Lukasz Pankowski =*=
Jul 18 '05 #4
Ivan Voras wrote:

Ok, I gather touples should be smaller & faster as I thought :)


Don't make a decision based on performance. Use tuples if you
require the immutability, or if you want to follow Guido's "advice" (?)
to treat them as simple unnamed structs.

Use lists any time you want lists of stuff, even if you think you
might benefit by the supposedly higher performance of a tuple.

Anyone reading your code will likely be much happier, and you won't
find yourself getting caught in a bind because you've used an
immutable tuples where you really should have used a list.

-Peter
Jul 18 '05 #5
Lukasz Pankowski wrote:
subsequent runs of of the script given below.


Thank you, this is exactly what I was looking for.
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #6
Peter Hansen wrote:
Ivan Voras wrote:

Ok, I gather touples should be smaller & faster as I thought :)


Don't make a decision based on performance. Use tuples if you
require the immutability, or if you want to follow Guido's "advice"
(?) to treat them as simple unnamed structs.


I know that. I was just wandering if they also behave faster. Lists are only
interesting to me in case I need mutability.
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #7
Ivan Voras wrote:

Peter Hansen wrote:
Ivan Voras wrote:

Ok, I gather touples should be smaller & faster as I thought :)


Don't make a decision based on performance. Use tuples if you
require the immutability, or if you want to follow Guido's "advice"
(?) to treat them as simple unnamed structs.


I know that. I was just wandering if they also behave faster. Lists are only
interesting to me in case I need mutability.


I'd suggest you have that backwards. Lists should _always_ be interesting
to you. Tuples should be interesting only in the case where you need
*immutability*.

-Peter
Jul 18 '05 #8
Peter Hansen wrote:
Ivan Voras wrote:

I know that. I was just wandering if they also behave faster. Lists
are only interesting to me in case I need mutability.


I'd suggest you have that backwards. Lists should _always_ be
interesting
to you. Tuples should be interesting only in the case where you need
*immutability*.


I disagree. :) I would always use tuples except when I explicitely need
mutability. That doesn't meen that I would intentionaly (badly) restructure
code just to use tuples, only that in many cases my structures don't need to
be modified.

--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #9
Ivan Voras wrote:

Peter Hansen wrote:
Ivan Voras wrote:

I know that. I was just wandering if they also behave faster. Lists
are only interesting to me in case I need mutability.


I'd suggest you have that backwards. Lists should _always_ be
interesting
to you. Tuples should be interesting only in the case where you need
*immutability*.


I disagree. :) I would always use tuples except when I explicitely need
mutability. That doesn't meen that I would intentionaly (badly) restructure
code just to use tuples, only that in many cases my structures don't need to
be modified.


That's your personal choice, I suppose, and I won't try any more to dissuade
you any more except to point out that most Python programmers, I believe, do
not see tuples as their first choice, but use them only in certain special cases.

I could be wrong. It happens. ;-)

-Peter
Jul 18 '05 #10
In article <3F***************@engcorp.com>,
Peter Hansen <pe***@engcorp.com> wrote:
Ivan Voras wrote:

Peter Hansen wrote:
Ivan Voras wrote:

> I know that. I was just wandering if they also behave faster. Lists
> are only interesting to me in case I need mutability.

I'd suggest you have that backwards. Lists should _always_ be
interesting
to you. Tuples should be interesting only in the case where you need
*immutability*.


I disagree. :) I would always use tuples except when I explicitely need
mutability. That doesn't meen that I would intentionaly (badly) restructure
code just to use tuples, only that in many cases my structures don't need
to
be modified.


That's your personal choice, I suppose, and I won't try any more to dissuade
you any more except to point out that most Python programmers, I believe, do
not see tuples as their first choice, but use them only in certain special
cases.


These two positions may be more similar than different. The
notion of a sequence that never needs to be modified is certainly
a special case, and if we were looking at the actual applications
for these sequences we might all agree that they're more or less
appropriately cast as tuples. One argues about principles like
this at some risk of silliness.

The thing that eventually pushed me towards lists and away from
tuples, for sequences of like items, is the notation. It may
seem like a awfully prosaic consideration, but lists are usually
easier to mentally parse out of an expression, because the tuple's
parentheses are shared by other syntactical groupings. And then
there's the awkward notation for a single-element tuple. Lists
are easier to write and easier to read.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
2
by: beliavsky | last post by:
Tuples, unlike lists, are immutable, which I crudely translate to mean "their contents cannot be changed". Out of habit, I use only lists, not tuples, in my code. But I wonder if this is poor style...
3
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
7
by: MTD | last post by:
Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = would...
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
27
by: seberino | last post by:
Please help me think of an example where immutable tuples are essential. It seems that everywhere a tuple is used one could just as easily use a list instead. chris
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.