Home > Backend Development > Python Tutorial > My Python program is too slow. How can I speed it up?

My Python program is too slow. How can I speed it up?

王林
Release: 2023-09-11 17:01:02
forward
750 people have browsed it

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 -

abstract

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

Avoid loop overhead

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 of

Example

is:

Example

Let’s look at an example of a loop

newlist = []
for word in oldlist:
   newlist.append(word.upper())
Copy after login

We can use map instead of the loop above to avoid the overhead−

newlist = map(str.upper, oldlist)
Copy after login

Use list comprehension

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]
Copy after login

Generator expression

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)
Copy after login

Local variables

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
Copy after login

Import statement overhead

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.

Connection string

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 of

Example

is:

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)
Copy after login

Output

0.003464221954345703
Copy after login

Use Join to connect multiple strings

The Chinese translation of

Example

is:

Example

We 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)
Copy after login

Output

0.000995635986328125
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template