current position:Home>How to make Python run faster? Six tips!
How to make Python run faster? Six tips!
2022-01-29 10:12:22 【StarOS_ Test】
Maybe Xiaobian has been paying attention to cloud computing for a long time 、 Knowledge of cloud primitives and other fields 、 Journalism , As soon as I open all kinds of We Media Software , All the courses pushed are related courses , among Python The most ( Maybe anything related to computer technology will be recommended ).
As a popular programming language ,Python There are many advantages : Easy to learn 、 The grammar is beautiful , It has a rich and powerful library , And it has a wide range of applications .
however ,Python It is not without shortcomings , One of the main disadvantages is Python The execution speed of is not fast enough . In response to this question , I've collected a few tips commonly used by the technical leaders of our team , I hope it can help you improve Python Operational efficiency .
Python Run faster Trick 1 : The key code uses the external function package ****
Python Simplifies many programming tasks , But for some time sensitive tasks , Its performance is often unsatisfactory . Use C/C++ Or the external function package of machine language to deal with time sensitive tasks , It can effectively improve the operation efficiency of applications .
These function packages are often attached to a specific platform , Therefore, you should choose the appropriate function package according to the platform you use . In short , This trick requires you to sacrifice the portability of the application in exchange for the efficiency that can only be achieved through direct programming of the underlying host .
Here are some feature packs you can choose to use to improve efficiency :
Cython
Pylnlne
PyPy
Pyrex
These function packs have different uses . for instance , Use C The data type of the language , It can make tasks involving memory operations more efficient or intuitive .
Pyrex Can help Python Extend such a function .Pylnline Can make you in Python It is directly used in the application C Code . Inline code is compiled independently , But it keeps all the compiled files somewhere , And make the most of C The efficiency provided by language .
Python Run faster Trick two : Use the key when sorting
Python Contains many ancient sorting rules , These rules take up a lot of time when you create custom sorting methods , When these sorting methods run, they will also delay the actual running speed of the program . The best way to sort is to use as many keys and built-in sort() Method . for example , Take the following code for example :
In each example ,list They are sorted according to the index you choose as the key parameter . This method is not only valid for numeric types , The same applies to string types .
Python Run faster Trick three : Optimization for cycles
Each programming language emphasizes the optimal loop scheme . When using Python when , You can use a wealth of techniques to make the loop run faster . However , One skill developers often forget is : Try to avoid accessing the properties of variables in a loop . for example , Take the following code for example :
Every time you call str.upper, Python Will calculate the value of this formula . However , If you assign this evaluation to a variable , Then the result of evaluation can be known in advance ,Python The program can run faster .
therefore , The key is to minimize Python The amount of work in the loop . because Python Explain the characteristics of execution , In the above example, it will greatly slow down .
( Be careful : There are many ways to optimize the loop , This is just one of them . such as , Many programmers will think , List derivation is the best way to improve the cycle speed . The key lies in , Optimizing the loop scheme is a good choice to improve the running speed of applications .)
Python Run faster Trick 4 : Use the newer Python edition
Usually , Each version Python Will include optimization content , Make it run faster than previous versions . however , The limiting factor is , Does your favorite function library have synchronous updates to support new Python edition . Instead of arguing about whether the function library should be updated , The key is the new Python Is the version efficient enough to support this update .
Make sure your code works in the new version . You need to use the new library to experience the new Python edition , Then you need to check your application when making key changes . Only after you have made the necessary corrections , You can understand the difference of the new version .
However , If you just make sure your app runs in the new version , You are likely to miss the new features provided by the new version . Once you decide to update , Please analyze the performance of your application in the new version , And check for possible problems , Then apply the new version of features to these parts first . That's the only way , Users can be aware of the improvement of application performance at the beginning of the update .
Python Run faster Tip 5 : Try a variety of coding methods
Every time you create an application, you use the same coding method, almost without exception, which will lead to unsatisfactory operation efficiency of the application . You can try some experimental methods in program analysis . for example , When dealing with data items in the dictionary , You can use both safe methods , Make sure the data item already exists before updating , You can also update data items directly , Treat non-existent data items separately as special cases . Look at the first code below :
When at first myDict It's empty time , This code will run faster . However , Usually ,myDict Filled with data , At least fill in most of the data , At this time, it will be more efficient to change another method .
The output is the same in both methods . The difference is how the output is obtained . Jump out of the conventional mode of thinking , Creating new programming skills can make your application more efficient .
Python Run faster Trick 6 : Cross compile your application
Developers sometimes forget that computers don't understand the programming language used to create modern applications . Computers understand machine language . To run your application , You use an application to convert your human readable code into machine-readable code . Sometimes , You use a method such as Python Writing applications in such a language , And then to C++ Such a language runs your application , From the operational point of view , It is feasible. . The key lies in , What do you want your app to do , And what kind of resources can your host system provide .
That's what makes you Python Six tips for running faster , I hope it can be a reference for all developers .
Free container cloud , Unlimited computing power 、 bandwidth :
Last , Xiaobian still recommends our team's cloud native platform to all developers as usual StarOS: Stamp link >
StarOS Not just a free container cloud platform , But a one-stop cloud native R & D platform , Container cluster with operation and maintenance free , Out of the box R & D facilities , Architecture design 、 The deployment environment 、 Language installation 、 Code development 、 Application testing 、 All applications will be deployed on the cloud native platform “ Pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull pull ” Realization ! The cloud native R & D platform is not just a container cloud , But a one-stop cloud native online development platform .
StarOS It is such a cloud native R & D platform , Everything is based on the cloud , Completely tube K8S, Make container services easy to use , Asset smoothing 、 Automatic extension .
If you also want to develop on the cloud , Let's experience ! Stamp link >>
dream , To do on the cloud !
copyright notice
author[StarOS_ Test],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291011564510.html
The sidebar is recommended
- [Python introduction project] use Python to generate QR code
- Compile D + +, and use d to call C from python
- Quickly build Django blog based on function calculation
- Python collects and monitors system data -- psutil
- Finally, this Python import guide has been sorted out. Look!
- Quickly build Django blog based on function calculation
- Python interface test unittest usage details
- Implementation of top-level design pattern in Python
- You can easily get started with Excel. Python data analysis package pandas (VII): breakdown
- Python simulation random coin toss (non optimized version)
guess what you like
-
Python tiktok 5000+ V, and found that everyone love this video.
-
Using linear systems in python with scipy.linalg
-
Using linear systems in python with scipy.linalg
-
Together with Python to do a license plate automatic recognition system, fun and practical!
-
You can easily get started with Excel. Python data analysis package pandas (XI): segment matching
-
Advanced practical case: Javascript confusion of Python anti crawling
-
Using linear systems in python with scipy.linalg
-
Fast power modulus Python implementation of large numbers
-
Quickly build Django blog based on function calculation
-
This paper clarifies the chaotic switching operation and elegant derivation of Python
Random recommended
- You can easily get started with Excel pandas (I): filtering function
- You can easily get started with Excel. Python data analysis package pandas (II): advanced filtering (I)
- You can easily get started with Excel. Python data analysis package pandas (2): advanced filtering (2)
- You can easily get started with Excel. Python data analysis package pandas (3): making score bar
- Test Development: self study Dubbo + Python experience summary and sharing
- You can easily get started with Excel. Python data analysis package pandas (V): duplicate value processing
- How does Python correctly call jar package encryption to get the encrypted value?
- Python 3 interview question: give an array. If there is 0 in the array, add a 0 after 0, and the overall array length remains the same
- Python simple Snake game (single player mode)
- Using linear systems in python with scipy.linalg
- Python executes functions and even code through strings! Come and understand the operation of such a top!
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- [Python introduction project] use Python to generate QR code
- Vanessa basks in her photos and gets caught up in the golden python. There are highlights in the accompanying text. She can't forget Kobe after all
- [windows] Python installation pyteseract
- [introduction to Python project] create bar chart animation in Python
- Fundamentals of Python I
- Python series tutorials 116
- Python code reading (chapter 35): fully (deeply) expand nested lists
- Practical series 1 ️⃣ Wechat applet automatic testing practice (with Python source code)
- Python Basics: do you know how to use lists?
- Solution of no Python 3.9 installation was detected when uninstalling Python
- [Python homework] coupling network information dissemination
- [common links of Python & Python]
- Python application software development tool - tkinterdesigner v1.0 5.1 release!
- [Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger
- [Python development tool Tkinter designer]: Lecture 2: introduction to Tkinter designer's example project
- [Python development tool Tkinter designer]: Lecture 1: introduction to the basic functions of Tkinter Designer
- [introduction to Python tutorial] use Python 3 to teach you how to extract any HTML main content
- Python socket implements UDP server and client
- Python socket implements TCP server and client
- leetcode 1261. Find Elements in a Contaminated Binary Tree(python)
- [algorithm learning] 1486 Array XOR operation (Java / C / C + + / Python / go / trust)
- leetcode 1974. Minimum Time to Type Word Using Special Typewriter(python)
- The mobile phone uses Python to operate picture files
- [learning notes] Python exception handling try except...
- Two methods of using pandas to read poorly structured excel. You're welcome to take them away
- Python sum (): the summation method of Python
- Practical experience sharing: use pyo3 to build your Python module
- Using Python to realize multitasking process