How should Python optimize code to speed up?

Original link: python speed-up skills

At work, we often face the problem of code speed-up optimization. This article introduces several Pythoncommon speed-up techniques for you.

Optimization principles:
1. Ensure that the code can run correctly before performing performance optimization.
2. The choice of optimization is usually to sacrifice space for time, so we need to weigh the cost.
3. Focus on optimizing the time-consuming part of the code, and overall optimization will usually reduce the code readability

0 Define time-consuming decorators

# 可监控程序运行时间
import time
def clock(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print("共耗时: %s秒" % round(end_time - start_time, 2))
        return result
    return wrapper

1 Avoid using global variables

start_time = time.time()
size = 10000
for x in range(size):
    for y in range(size):
        z = x * y
end_time = time.time()
print('共耗时:%s秒' % round(end_time - start_time, 2))

# 共耗时:11.78秒,不推荐
# 使用局部变量
@clock
def multiplication():
    size = 10000
    for x in range(size):
        for y in range(size):
            z = x * y
multiplication()

# 共耗时: 5.5秒,提速50%

2 Avoid accessing the properties of methods (functions), try to use from X import X

import math
@clock
def computeSqrt():
    result = []
    for i in range(10000000):
        # math方法访问sqrt属性
        result.append(math.sqrt(i))
computeSqrt()

# 不推荐,共耗时: 2.09秒
# 使用from X import X,直接访问sqrt
from math import sqrt
@clock
def computeSqrt():
    result = []
    for i in range(10000000):
        result.append(sqrt(i))
computeSqrt()

# 推荐,共耗时: 1.75秒

In [1] we mentioned that local variable lookup will be faster than global variables, so for frequently accessed variables append, changing them to local variables can speed up the operation.

from math import sqrt
@clock
def computeSqrt():
    result = []
    # 赋值给局部变量
    append = result.append
    for i in range(10000000):
        append(sqrt(i))
computeSqrt()

# 推荐,共耗时: 1.45秒

3 Traversal optimization

# 使用while进行遍历
@clock
def circulate():
    i = 0
    li = []
    append = li.append
    while i < 10000000:
        append(i*2)
        i += 1
    return li
circulate()

# 不推荐,共耗时:1.48秒
@clock
def circulate():
    li = []
    append = li.append
    # 使用for代替while
    for i in range(10000000):
        append(i*2)
    return li
circulate()

# for优于while,共耗时:1.09秒
@clock
def circulate():
    # 使用列表推导式
    return [i*2 for i in range(10000000)]
circulate()

# 推荐列表推导式,共耗时:0.88秒。但不适用于复杂计算。

4 Reduce the calculation of the inner for loop

from math import sqrt
@clock
def inner():
    size = 10000
    for x in range(size):
        for y in range(size):
            # 相当于在重复计算sqrt(x)
            z = sqrt(x) + sqrt(y)
inner() 

# 不推荐,共耗时:19.00秒
from  math import sqrt
@clock
def inner():
    size = 10000
    for x in range(size):
        # 只计算一次sqrt(x),然后将它存了起来
        sqrt_x = sqrt(x)
        for y in range(size):
            z = sqrt_x + sqrt(y)
inner() 

# 推荐,共耗时:10.22秒

5 NumpyOperations using data types

Because Numpythe bottom layer is C语言implemented in , and this scripting language has natural disadvantages in terms of efficiency and performance Pythoncompared to this compiled language, we can introduce packages to convert the data before performing calculations.C/C++Numpy

import numpy as np

li = [i for i in range(10000000)]

@clock
def npSpeed():
    # 使用Python方法
    sum(li)
npSpeed() 

# 共耗时0.79秒
import numpy as np

li = np.array([i for i in range(100000000)])
@clock
def npSpeed():
    # 使用Numpy方法
    np.sum(li)
npSpeed() 

# 共耗时0.11秒,速度约是Python的8倍

NumpyThe advantages will be more obvious when the amount of data is larger. NumpyTherefore, it is used very frequently in machine learning and deep learning project tasks .

Original link: python speed-up skills

More dry goods related to AI deep learning are available on the official account: AI has temperature
insert image description here

Guess you like

Origin blog.csdn.net/Antai_ZHU/article/details/121117437