Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tuples vs lists

Reply
Thread Tools

tuples vs lists

 
 
Ivan Voras
Guest
Posts: n/a
 
      09-16-2003
Are there any performance/size differences between using tuples and using
lists?


--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke


 
Reply With Quote
 
 
 
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      09-16-2003
On Tue, 16 Sep 2003 18:51:38 +0200, rumours say that "Ivan Voras"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Lukasz Pankowski
Guest
Posts: n/a
 
      09-16-2003
"Ivan Voras" <(E-Mail Removed)> 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 =*=
 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      09-16-2003
Ok, I gather touples should be smaller & faster as I thought


--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke


 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      09-16-2003
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


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      09-16-2003
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
 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      09-16-2003
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


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      09-16-2003
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
 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      09-17-2003
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


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      09-17-2003
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
 
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
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Passing tuples of tuples to sqlite xera121 Python 8 09-30-2009 05:45 AM
tuples within tuples korovev76@gmail.com Python 12 10-27-2007 08:16 PM
Re: Lists of lists and tuples, and finding things within them Daniel Nogradi Python 3 11-10-2006 07:57 AM
Different tuples to one container? (One type of a pointer to point to different kinds of tuples?) fff_afafaf@yahoo.com C++ 5 10-05-2006 11:17 PM



Advertisments