Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve the running speed of python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report