- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
My Python program is too slow. How do I speed it up?
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
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.
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 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)
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.
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 −
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)
Concatenate Many Strings using the Join
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)
- Related Articles
- Speed up a slow pc without spending money
- Why do I sweat too much, Is it bad for me?
- How do I check what version of Python is running my script?
- How do I correctly clean up a Python object?
- How do I find the location of my Python site-packages directory?
- Do Steroid Meds Up My Risk of COVID-19 or Getting Sicker from It?
- How Do I Make It So My Table Doesn\'t Format \"Wrong\" In HTML?
- What environment variables do I need to set up before I start running Java programs on my machine?
- MongoDB 'count()' is very slow. How do we work around with it?
- How do I make my string comparison case insensitive?
- How Do I Promote My Business Through Digital Marketing?
- How can we speed up Python "in" operator?
- How do I program using threads in Python
- What Should I Do If My IP Address Is Exposed?