Get the App
SLTechnology News&Howtos  ›  Development  › 

How to improve the running speed of python

Shulou Source: shulou.com Published: 2022-06-03 16:44:23 10月02日 Update

This article is about how to speed up Python. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

First, analyze the code running time

Formula 1, measure code runtime

trivial method

Shortcuts (jupyter environment)

Formula 2: Calculate the average running time of code for many times

trivial method

Shortcuts (jupyter environment)

Formula 3: Analyze code runtime by calling function

trivial method

Shortcuts (jupyter environment)

Formula 4, Analyze code runtime by line

trivial method

Shortcuts (jupyter environment)

Second, speed up your search.

5. Use a set instead of a list.

low-speed method

high speed method

Formula 6: Use dict instead of two lists for matching.

low-speed method

high speed method

Three, speed up your cycle.

7. Use the for loop rather than the while loop.

low-speed method

high speed method

Equation 8: Avoid double counting in the loop body

low-speed method

high speed method

Four, speed up your function.

Equation 9: Replace recursive function with loop mechanism

low-speed method

high speed method

Equation 10: Speed up recursive functions with caching mechanisms

low-speed method

high speed method

Formula 11, using numba to speed up Python functions

low-speed method

high speed method

Fifth, use standard library functions for acceleration

Formula 12, use collections.Counter to accelerate counting

low-speed method

high speed method

Formula 13, use collections.ChainMap to accelerate dictionary merging

low-speed method

high speed method

VI. Use higher-order functions for acceleration

Equation 14: Use map instead of derivation to accelerate

low-speed method

high speed method

Equation 15: Use filter instead of derivation to accelerate

low-speed method

high speed method

Seven, use numpy vectorization for acceleration

16, use np.array instead of list

low-speed method

high speed method

In equation 17, use np.ufunc instead of math.func

low-speed method

high speed method

18, use np.where instead of if

low-speed method

high speed method

Eight, speed up your Pandas

Formula 19, use csv file to read and write instead of excel file to read and write

low-speed method

high speed method

Formula 20, using the pandas multiprocess tool pandartle

low-speed method

high speed method

9. Use Dask to accelerate

Equation 21: Use dask to accelerate dataframe

low-speed method

high speed method

Equation 22: Use dask.delayed to accelerate

low-speed method

high speed method

Ten, apply multi-threaded multi-process acceleration

Formula 23, apply multithreading to accelerate IO-intensive tasks

low-speed method

high speed method

Formula 24: Apply multi-process acceleration to CPU-intensive tasks

low-speed method

high speed method

Thank you for reading! About "how to improve python running speed" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Tags: Method low speed high speed run function code time loop trivial environment process analysis application speed task content intensive file more mechanism Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Linux Docker MySQL