"Pure dry" tips for improving the efficiency of Python operations

Source: Internet
Author: User

Python is an excellent language that allows you to do a lot of things in a short amount of code. Not only that, it also easily supports multitasking, such as multi-process.

People who don't like python often make noises. Python runs too slowly. But that is not the case. Try the following six tips to speed up your Python app.

Tip One: The key code uses the external Feature pack

Python simplifies many programming tasks, but its performance is often unsatisfactory for some time-sensitive tasks. The use of the external feature pack, in C + + or machine language, to handle time-sensitive tasks can effectively improve the efficiency of your application. These feature packs are often attached to a particular platform, so you have to choose the right Feature pack based on the platform you're using. In short, the trick is to sacrifice the portability of your app in exchange for operational efficiencies that can only be achieved through direct programming to the underlying host. Here are some of the feature packs you can choose to use to improve efficiency:

Cython

Pylnlne

PyPy

Pyrex

These feature packs are useful in different ways. For example, using the C-language data type can make tasks involving memory operations more efficient or more intuitive. Pyrex can help Python extend this feature. Pylnline allows you to use C code directly in Python applications. The inline code is compiled independently, but it keeps all the compiled files somewhere and takes full advantage of the high efficiency provided by the C language.

Tip Two: Use keys when sorting

Python has many ancient collations that take a lot of time when you create custom sorting methods, and these sort methods also delay the actual speed at which the program runs. The best way to sort is to use the keys and the built-in sort () method as much as possible. For example, take the following code:

In each case, the list is sorted based on the index you choose to use as the key parameter. This method is not only valid for numeric types, but also for string types.

Tip Three: Optimizing for loops

Each programming language emphasizes the optimization of the loop scheme. When using Python, you can use a variety of techniques to make the loop program run faster. However, one of the tricks that developers often forget is to try to avoid accessing the properties of variables in a loop. For example, take the following code:

Every time you call Str.upper, Python calculates the value of this equation. However, if you assign this value to a variable, the result of the evaluation can be known in advance, and the Python program can run faster. Therefore, the key is to minimize the amount of Python work in the loop. Because Python interprets the characteristics of execution, it slows it down considerably in the example above.

(Note: There are many more ways to optimize loops, which is just one of them.) For example, many programmers would argue that a list derivation is the best way to improve the speed of a loop. The key is that optimizing the loop scheme is a good choice for increasing the speed of your application. )

Tip Four: Use a newer version of Python

If you search for Python online, you'll find countless messages about how to upgrade the Python version. In general, each version of Python will contain optimized content, making it run faster than previous versions. However, the limiting factor is that your favorite library has no sync updates to support the new Python version. Rather than arguing whether the library should be updated, the key is whether the new Python version is efficient enough to support this update.

You have to make sure that your code will work in the new version. You need to use the new library to experience the new Python version, and then you need to check your app when making critical changes. Only when you have completed the necessary corrections will you be able to appreciate the differences in the new version.

However, if you just make sure your app works in the new version, you'll probably miss out on the new features offered by the new version. Once you decide to update, analyze the performance of your app under the new version and check for possible problems, and then prioritize the features of the new version for those parts. Only then can the user be aware of the change in application performance at the beginning of the update.

Tip Five: Try multiple coding methods

Using the same encoding method every time you create an app can make your app run less efficiently. You can try some experimental methods when you analyze your program. For example, when working with data items in a dictionary, you can either use a secure method, make sure that the data item already exists, update it, or update the data item directly, and separate the nonexistent data items as exceptions. Take a look at the first piece of code below:

This code runs faster when the initial mydict is empty. In general, however, Mydict fills up the data, with at least the bulk of the data, and a different approach is more efficient.

The output is the same in both methods. The difference is how the output is obtained. Jump out of the normal mindset and create new programming techniques to make your app more efficient.

Tip Six: Cross-compiling your app

Developers sometimes forget that computers do not actually understand the programming language used to create modern applications. The computer understands machine language. To run your app, you can use an app to convert your human-readable code into machine-readable code. Sometimes, you write your app in a language such as Python, and then run your app in a language like C + +, which is feasible at the operational point of view. The key is what you want your app to accomplish and what resources your host system can provide.

Nuitka is an interesting cross-compiler that translates your Python code into C + + code. This way, you can execute your own app in native mode without having to rely on the interpreter program. You will find that your application is running more efficiently, but this will vary depending on the platform and the task.

(Note: Nuitka is still in the testing phase, so be careful in your application.) In fact, it's best to use it for experimentation at the moment. In addition, there is room for discussion about whether cross-compilation is the best way to improve operational efficiency. Developers have used cross-compiling for years to improve the speed of applications. Keep in mind that every solution has pros and cons, so weigh it carefully before you use it in your production environment. )

When using the cross compiler, make sure that it supports the version of Python you are using. Nuitka supports Python2.6, 2.7, 3.2 and 3.3. In order for the solution to take effect, you need a Python interpreter and a C + + compiler. Nuitka supports many C + + compilers, including Microsoft Visual Studio,mingw and CLANG/LLVM.

Cross-compiling can cause some serious problems. For example, when using Nuitka, you will find that even a small program consumes a huge amount of drive space. Because Nuitka uses a series of dynamic link libraries (DDLs) to perform Python functions. So, if you're using a system with a very limited resource, this approach might not be feasible.

Conclusion

The six tips described above can help you create Python apps that run more efficiently. But the silver bullet doesn't exist. These tips don't necessarily work every time. Some of the tricks may be better than others in a particular Python version, but this sometimes depends on the platform's differences. You need to summarize and analyze your application, find its inefficient parts, and then try these tips to find the best way to solve the problem.

http://www.biyinjishi.com/products/a50-b5065/
http://www.biyinjishi.com/products/a50-b5070/
http://www.biyinjishi.com/products/a50-b5099/
http://www.biyinjishi.com/products/a60-b6010/
http://www.biyinjishi.com/products/a60-b6020/

"Pure dry" tips for improving the efficiency of Python operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.