Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the tips for Python performance tuning

Shulou Source: shulou.com Published: 2022-06-02 13:27:35 09月22日 Update

This article mainly shows you "what are the tips for Python performance tuning", which is easy to understand and organized. I hope it can help you solve your doubts. Let me lead you to study and learn "what are the tips for Python performance tuning".

1 use list generation more often

Replace the following code:

Cube_numbers = [] for n in range (0Jing 10): if n% 2 = = 1: cube_numbers.append (nasty 3)

Write for list generation:

Cube_numbers = [naughtified 3 for n in range (1Magne10) if n% 2 = = 1] 2 built-in function

Use as many of these built-in functions as possible:

3 use generators whenever possible

When dealing with a large amount of data on a single machine, the generator is often useful because it is read in small pieces one by one to maximize memory savings, such as using yield when crawling web pages

Import requestsimport redef get_pages (link): pages_to_visit = [] pages_to_visit.append (link) pattern = re.compile ('https?') While pages_to_visit: current_page = pages_to_visit.pop (0) page = requests.get (current_page) for url in re.findall ('

Tags: Variables functions generation performance skills global content writing modules articles fastest intersection code memory multiple as much as possible local generator cache recursion Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft OPPO Reno Shulou Information Docker