
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Speed Up Your Slow Python Program
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 to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower
Avoid Looping Overhead
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.
Example
Let us see an example of a loop
newlist = [] for word in oldlist: newlist.append(word.upper())
We can use map instead to avoid the overhead of the above loop ?
newlist = map(str.upper, oldlist)
Use List Comprehension
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
Generator expressions were introduced in Python 2.4. These are considered the best alternative of looping since it avoids 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)
Local Variables
Python accesses local variables much more efficiently than global variables. We can implement the above example using the local variables itself ?
def func(): upper = str.upper newlist = [] append = newlist.append for word in oldlist: append(upper(word)) return newlist
Import Statement Overhead
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.
Concatenating Strings
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
Example
We will now concatenate many strings and check the execution time using the time module ?
from time import time myStr ='' a='gjhbxjshbxlasijxkashxvxkahsgxvashxvasxhbasxjhbsxjsabxkjasjbxajshxbsajhxbsajxhbasjxhbsaxjash' l=[] # Using the + operator t=time() for i in range(1000): myStr = myStr+a+repr(i) print(time()-t)
Output
0.003464221954345703
Concatenate Many Strings using the Join
Example
We will now concatenate many strings using Join and check the execution time. When we have many strings, the join is a better and faster option ?
from time import time myStr ='' a='gjhbxjshbxlasijxkashxvxkahsgxvashxvasxhbasxjhbsxjsabxkjasjbxajshxbsajhxbsajxhbasjxhbsaxjash' l=[] # Using the + operator t=time() for i in range(1000): l.append(a + repr(i)) z = ''.join(l) print(time()-t)
Output
0.000995635986328125