If your Python program is too slow, you can follow the below given tips and tricks − Abstraction Avoid excessive abstraction, especially under the form of tiny functions or method. Abstractions tend to create indirections and force the interpreter t...
If your Python program is too slow, you can follow the below given tips and tricks −
Avoid excessive abstraction, especially under the form of tiny functions or method. Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower
If the body of your loop is simple, the interpreter overhead of the for loop itself can be a substantial amount of the overhead. This is where the map function works in a better way. The only restriction is that the loop body of map must be a function call.
Let us see an example of a loop
We can use map instead to **oid the overhead of the above loop −
newlist = map(str.upper, oldlist)
The usage of List Comprehension uses less overhead than the for loop Let’s see the same example implemented using the List Comprehension −
newlist = [s.upper() for s in oldlist]
Generator expressions were introduced in Python 2.4. These are considered the best alternative of looping since it **oids the overhead of generating the entire list at once. Instead, they return a generator object which can be iterated over bit-by-bit −
iterator = (s.upper() for s in oldlist)
Python accesses local variables much more efficiently than global variables. We can implement the above example using the local variables itself −
The import statements can be executed easily. It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Repeatedly executing an import statement can seriously affect performance in some circumstances.
When concatenating many strings using Join, it is a better and faster option. However, when the strings are not more, using the + operator for concatenation is more efficient. It takes less time for execution. Let’s see this with two examples −
Concatenate Many Strings using the + Operator
We will now concatenate many strings and check the execution time using the time module −
0.003464221954345703
Concatenate Many Strings using the Join
We will now concatenate many strings using Join and check the execution time. When we h**e many strings, the join is a better and faster option −
0.000995635986328125