testingdebuggingprofilingand tuning tuning strategies the following sections outline few optimization strategies thatin the opinion of the authorhave proven to be useful with python code understand your program before you optimize anythingknow that speedup obtained by optimizing part of program is directly related to that part' total contribution to the execution time for exampleif you optimize function by making it run times as fast but that function only contributes to percent of the program' total execution timeyou're only going to get an overall speedup of about %- depending on the effort involved in making the optimizationthis may or may not be worth it it is always good idea to first use the profiling module on code you intend to optimize you really only want to focus on functions and methods where your program spends most of its timenot obscure operations that are called only occasionally understand algorithms poorly implemented ( log nalgorithm will outperform the most finely tuned ( algorithm don' optimize inefficient algorithms--look for better algorithm first use the built-in types python' built-in tuplelistsetand dictionary types are implemented entirely in and are the most finely tuned data structures in the interpreter you should actively use these types to store and manipulate data in your program and resist the urge to build your own custom data structures that mimic their functionality (that isbinary search treeslinked listsand so onhaving said thatyou should still look more closely at types in the standard library some library modules provide new types that outperform the built-ins at certain tasks for instancethe collection deque type provides similar functionality to list but has been highly optimized for the insertion of new items at both ends listin contrastis only efficient when appending items at the end if you insert items at the frontall of the other elements need to be shifted in order to make room the time required to do this grows as the list gets larger and larger just to give you an idea of the differencehere is timing measurement of inserting one million items at the front of list and dequefrom timeit import timeit timeit(' appendleft( )''import collectionss collections deque()'number= timeit(' insert( , )'' []'number= don' add layers any time you add an extra layer of abstraction or convenience to an object or functionyou will slow down your program howeverthere is also trade-off between usability and performance for instancethe whole point of adding an extra layer is often to simplify codingwhich is also good thing lib fl ff