Insights from Our Experts
Python optimization techniques
Coding Standard is an important criteria for the career of a Developer. In this, Code Optimisation is a must. Here are some techniques for the developers to optimize the code of Python Programming Language.
We can simply define a lambda function as a function without a name i.e, an anonymous function. It can be used with the following functions as well: filter(), map() and reduce().
Simple Example :
Here the first parameter of the map() is a function and other one is a list.
Here also the first parameter of the filter() is a function and the other one is a list.
Here it filters the set of odd numbers from the list
Similar to previous functions, the first parameter of the reduce() is a function and the other one is a list.
Flow of reducing function is given below:
p = 1,2,3,4
step 1 = 1+2=3
step 2= step 1 + 3 = 6
step 3= step 2 + 4 = 10
List comprehension can be used in many cases as a substitute for lengthy codes. It is basically used for creating Lists.
Here is an example:-
This code takes almost 6 lines, we can standardise the code and reduce the number of lines.
OPTIMISED CODE using LIST COMPREHENSION-
List Comprehension can be used in many other cases like substituting map(), filter() etc.
Dictionary comprehension is used for optimizing the code in case of creating a dictionary.
Using Imports Properly
Consider a scenario in which a particular package needs to be imported from a particular module. If we specify the package and module then it is optimised
We can specifically import the required thing and can avoid importing the entire package.
Using Lazy Generators
Consider the code given below:
Here in the first section of the code by using range, we built 100 elements in the memory and found its sum. This is a waste of memory. So in order to optimize the code, xrange creates a lazy generator, when we use the xrange it generate each number in which sum will consume to accumulate the sum. So memory will not be wasted and hence code will be optimized.
Use ‘try except’ Instead of ‘if else’
In the second case, the interpreter executes the sub() first and throws an exception in the exceptional case but in the first case the interpreter has to search each time the ‘if statement’ is executed, because the name could refer to something different since the last time the statement was executed. It is better to use the try except for a better optimization.
It is used to get an analysis of a program which is executed, i.e how often or how long it gets used. We can use several profiling modules in a profile and the function execution gets easy. Suppose your main function say sample() takes no argument and you can execute this under the control of profile module.
It will print a table of function calls corresponding to the execution times .
Speed of execution will be higher from the normal way.