File: class/Workbook/Examples/Lecture9/testinline.py

#!/opt/local/bin/python
import time
from sys import argv, exit
numtests = 20
 
try:
    pushes, pops = eval(argv[1]), eval(argv[2])
except:
    print 'usage: testinline.py <pushes> <pops>'; exit(1)
 
def test(reps, func):
    start_cpu  = time.clock()
    for i in xrange(reps):                # call N times
        x = func()
    return time.clock() - start_cpu
 
def inline1():                            # builtin lists
    x = []
    for i in range(pushes): x.append('spam' + `i`)
    for i in range(pops):   del x[-1]
 
def inline2():                            # builtin tuples
    x = None
    for i in range(pushes): x = ('spam' + `i`, x)
    for i in range(pops):   (top, x) = x
 
print 'lists: ', test(numtests, inline1)  # run 20 times
print 'tuples:', test(numtests, inline2)
 



[Home page] Books Code Blog Python Author Train Find ©M.Lutz