[Python-Dev] Ridiculously minor tweaks?

Jeremy Fincher fincher.8@osu.edu
Tue, 11 Mar 2003 20:03:08 -0500

On Tuesday 11 March 2003 09:11 pm, Guido van Rossum wrote:
> I bet you can't prove the speed-up.

Here's the script I used to test it:

import timeit

report = \
Size: %s
Tuple Time: %s
List Time: %s
List Time - Tuple Time: %s

if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        upperLimit = sys.argv[1]
        upperLimit = 10
    for i in xrange(upperLimit):
        lst = range(i)
        tpl = tuple(lst)
        tupleTimer = timeit.Timer('%s in %r' % (upperLimit, tpl))
        listTimer = timeit.Timer('%s in %r' % (upperLimit, lst))
        minTupleTime = min(tupleTimer.repeat())
        minListTime = min(listTimer.repeat())
        difference = minListTime - minTupleTime
        print report % (i, minTupleTime, minListTime, difference)

There seems to be a constant 1.3 usec or so difference between creating a 
tuple and creating a list.  As I mentioned earlier, I seriously doubt it 
would have any significant impact on the overall execution speed of any 
non-trivial Python program, but it exists nonetheless.  Maybe in the realm of 
'low hanging fruit' it's the fruit that's fallen to the ground and begun to 
rot :)

> Tuples are for heterogeneous data, list are for homogeneous data.
> Tuples are *not* read-only lists.

I understand this in a strictly typed language, but in Python, since lists can 
be just as heterogeneous as tuples, it's always seemed to me that the 
greatest difference between lists and tuples is the mutability and 
extensibility of lists.