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 :

1.png

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 :

2.png

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 :

3.png

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 .

4.png

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 .

Test entrance >>

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

Random recommended