#!/usr/bin/env python2.4
"""
Compare accessing elements of list, array and deque
"""

import sys
import time
import array
from collections import deque
import random
random.seed()


def testRandomAccess(collection, size):
    """ Test accessing items randomally """
    for i in xrange(size):
        collection.append(i)

    lastIndex = len(collection) - 1
    for i in xrange(size):
        index = random.randint(0, lastIndex)
        dummy = collection[index]


def testAppendAndRemove(collection, size):
    """ Test a adding and removing items """
    for i in xrange(size):
        collection.append(i)

    while collection:
        del collection[0]


def test(function):
    print
    print 'size       list       array      deque'
    print '-------------------------------------------'
    
    for size in (10, 1000, 10000, 100000, 200000):
        print '%10d' % size,
        
        for collection in (list(), array.array('i'), deque()):
            start = time.time()
            function(collection, size)
            print '%10.6f' % (time.time() - start),

        print


# Run the tests
print
print 'Adding and removing items'
test(testAppendAndRemove)
print
print 'Accessing items randomally'
test(testRandomAccess)
