Python is an excellent language that allows you to do a lot of things in a short amount of time with very little code. Not only that, it also easily supports multitasking, such as multiple processes. People who don’t like Python often make noise when Python is slow. But that’s not the case. As a program monkey is hoping to optimize the code, simplify the code. Here are six Python code optimization tips from the Dark Horse Programmer Python discipline. Because of GIL, Python has a hard time taking full advantage of multicore cpus. However, the following parallel patterns can be implemented through the built-in module Multiprocessing:


1. Multi-process parallel programming


For CPU-intensive programs, you can use packaged classes such as Process and Pool of Multiprocessing to realize parallel computing through multi-process. However, due to the high cost of communication in the process, the efficiency of programs requiring a large amount of data interaction between processes may not be greatly improved.


2. Multi-threaded parallel programming


Dummy module the dummy module uses the interface from Multiprocessing to wrap threading for IO intensive programs, making multithreaded programming very easy (for example, using the Pool map interface). Distributed: The Managers class in Multiprocessing provides a way to share data among different processes on which to build distributed programs. Different business scenarios can choose one or a combination of them to optimize program performance.


3. Optimize the algorithm time


The time complexity of an algorithm has the greatest impact on the execution efficiency of a program. In Python, the time complexity can be optimized by choosing an appropriate data structure, for example, the time complexity of a list and a set to find an element is O(n) and O(1), respectively. Different scenarios have different optimization methods. In general, there are ideas of divide and conquer, branch boundaries, greed, dynamic programming and so on. The union, intersection, difference operations of a set are faster than the iteration of a list. So if the problem involves finding the list intersection, union, or difference, it can be converted to a set.

4. Optimization for loops


Every programming language emphasizes the need to optimize loops. When using Python, you can rely on a number of tricks to make loops run faster. However, one approach that developers often leave out is to avoid using dot actions in a loop. For example, consider the following code: Every time you call the method str.upper, Python evaluates that method. However, if you replace the evaluated value with a variable, the value becomes known, and Python can perform its task much faster. The key to tuning loops is to reduce the amount of work Python has to do inside loops, because Python’s native interpreter really slows down execution in that case. lowerlist = [‘this’, ‘is’, ‘lowercase’] upper = str.upper upperlist = [] append = upperlist.append for word in lowerlist: append(upper(word)) print(upperlist) #Output = [‘THIS’, ‘IS’, ‘LOWERCASE’]


5. Function selection


Use xrange instead of range when looping; Using Xrange saves a lot of system memory because xrange() produces only one integer element per call in the sequence. Range (), on the other hand, returns the complete list of elements directly, which is unnecessarily expensive to loop through. Xrange no longer exists in python3, where range provides an iterator that can go over any range of length.


6. Use performance analysis tools


In addition to the timeit module used in Ipython above, there is also cProfile. CProfile is also very simple to use: Python -m cProfile filename.py, filename.py is the filename of the program to run. You can see in standard output the number of times each function is called and the running time to find performance bottlenecks in the program, which can then be optimized accordingly.


Follow public accounts

【 Pegasus Club 】


Previous welfare concerns about the pegasus public number, reply to the corresponding keywords package download learning materials; Reply “join the group”, join the Pegasus AI, big data, project manager learning group, and grow together with excellent people!

Microsoft Danniu artificial intelligence series of lessons

(Scan or subscribe)



M.qlchat.com/live/channe… (Qr code automatic recognition)



From beginning to research, the 10 most Readable books in the field of artificial intelligence

RSVP number “2” machine learning & Data Science must-read classic book with resource pack!

Into AI & ML: Learning machine Learning from Basic Statistics (PDF download)

Answer the number “4” to learn about ARTIFICIAL intelligence, 30 books should not be missed (with electronic PDF download)

Answer number “6” AI AI: 54 Industry Blockbuster Reports

TensorFlow Introduction, Installation tutorial, Image Recognition application (with installation package/guide)

According to a 160-page McKinsey report, 800 million people around the world could lose their jobs to machines by 2030

Reply number “12” small white | Python + + machine learning Matlab neural network theory + practice + + + depth video + courseware + source code, download attached!

Reply number “14” small white | machine learning and deep learning required books + machine learning field video/PPT + large data analysis books recommend!

Reply to the number “16” 100G Python from beginner to Master! Complete video tutorials + Python Classics for self-study!

Answer number “17” 【 dry article 】31 papers on deep learning required reading

526 Industry reports + White papers: AI, Artificial intelligence, robotics, smart mobility, smart home, Internet of Things, VR/AR, blockchain, etc. (download)

Reply number “19” 800G ARTIFICIAL intelligence learning materials :AI ebook +Python language introduction + tutorial + machine learning and other limited time free access!

17 mind maps for machine learning statistics

Ten years ago on This day on Machine Learning Projects.

Machine learning: How to go from beginner to Never Giving up? (With benefits)

Respond to digital “24” flash download | 132 g programming data: Python, JAVA, C, C + +, robot programming, PLC, entry to the proficient in ~

Reply number “25” limited resources | 177 g Python/machine learning/TensorFlow video/deep learning algorithm, introduction to cover/intermediate/project each stage!

Reply number “26” introduction to artificial intelligence book list recommended, learn AI please collect well (attached PDF download)

Reply | digital “27” Wu En of Stanford CS230 deep learning course a full range of information release (download)

FMI Artificial Intelligence and Big Data Summit Guest Speech PPT

Top 10 AI Jianghu Fields

Machine Learning Practical Experience Guide

More than 100 Papers on deep Learning

Top ten Classic Algorithms of Data Mining

6.10 Ele. me & Pegasus Project Management Practice PPT