tuples vs lists

Lukasz Pankowski lupan at zamek.gda.pl
Tue Sep 16 20:15:30 CEST 2003


"Ivan Voras" <ivoras at 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 at 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 =*=




More information about the Python-list mailing list