top( timing the improvements the prior section' in-place changes stack object probably runs faster than both the original and the tuple-tree versionsbut the only way to really be sure is to time the alternative implementations since this could be something we'll want to do more than oncelet' first define general module for timing functions in python in example - the built-in time module provides clock function that we can use to get the current cpu time in floating-point secondsand the function timer test simply calls function reps times and returns the number of elapsed seconds by subtracting stop from start times example - pp \dstruct\basic\timer py "generic code timer tooldef test(repsfunc*args)import time start time clock(for in range(reps)func(*argsreturn time clock(start or best of nsee learning python current cpu time in float seconds call function reps times discard any return value stop time start time there are other ways to time codeincluding best-of- approach and python' own timeit modulebut this module suffices for our purpose here if you're interested in doing bettersee learning pythonfourth editionfor larger case study on this topicor experiment on your own nextwe define test driver script which deploys the timerin example - it expects three command-line argumentsthe number of pushespopsand indexing operations to perform (we'll vary these arguments to test different scenarioswhen run at the top levelthe script creates instances of the original and optimized stack classes and performs the specified number of operations on each stack pushes and pops change the stackindexing just accesses it example - pp \dstruct\basic\stacktime py "compare performance of stack alternativesimport stack import stack list-based stacks[ ]+ tuple-tree stacks( ,ybecause python is so dynamicguesses about relative performance in python are just as likely to be wrong as right moreovertheir accuracy is prone to change over time trust me on this 've made sweeping statements about performance in other booksonly to be made wrong by later python release that optimized some operations more than others performance measurement in python is both nontrivial and an ongoing task in generalcode for readability firstand worry about performance laterbut always gather data to support your optimization efforts implementing stacks