Home > Article > Backend Development > My Python program is too slow. How can I speed it up?
If your Python program is too slow, you can follow the tips and tricks given below -
Avoid excessive abstraction, especially in the form of tiny functions or methods. Abstraction tends to create indirection and force the interpreter to do more work. If the level of indirection exceeds the amount of useful work done, your program will slow down
If the loop body is very simple, the interpreter overhead of the for loop itself may account for a large part of the overhead. This is where the map function works better. The only restriction is that the loop body of map must be a function call.
The Chinese translation ofLet’s look at an example of a loop
newlist = [] for word in oldlist: newlist.append(word.upper())
We can use map instead of the loop above to avoid the overhead−
newlist = map(str.upper, oldlist)
Using list comprehensions uses less overhead than for loops. Let’s look at the same example implemented using list comprehensions -
newlist = [s.upper() for s in oldlist]
Generator expressions were introduced in Python 2.4. These are considered the best alternative to loops as it avoids the overhead of generating the entire list at once. Instead, they return a generator object that can be iterated bit by bit -
iterator = (s.upper() for s in oldlist)
Python accesses local variables more efficiently than global variables. We can implement the above example using local variables themselves -
def func(): upper = str.upper newlist = [] append = newlist.append for word in oldlist: append(upper(word)) return newlist
Import statements can be easily executed. It is often useful to place them inside functions to limit their visibility and/or reduce initial startup time. In some cases, repeated execution of import statements can severely impact performance.
This is a better and faster option when concatenating multiple strings using Join. However, when there are not many strings, it is more efficient to use the operator to concatenate. It takes less time to execute. Let's look at this with two examples.
Use operator to connect multiple strings
The Chinese translation ofWe 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)
0.003464221954345703
Use Join to connect multiple strings
The Chinese translation ofWe will now use Join to concatenate many strings and check the execution time. When we have many strings, concatenation 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)
0.000995635986328125
The above is the detailed content of My Python program is too slow. How can I speed it up?. For more information, please follow other related articles on the PHP Chinese website!