Six tips for improving Python Running Efficiency

Source: Internet
Author: User

Six tips for improving Python Running Efficiency

Python is an excellent language that allows you to perform many operations with a very small amount of code in a short time. In addition, it also easily supports multi-task processing, such as multi-process.

People who do not like Python often vomit that Python is too slow to run. However, this is not the case. Try the following six tips to speed up your Python application.

Tip 1: use external function packages for key code

Python simplifies many programming tasks, but its performance is often unsatisfactory for time-sensitive tasks. Using an external function package in C/C ++ or machine language to process time-sensitive tasks can effectively improve the running efficiency of applications. These feature packages are often attached to specific platforms. Therefore, you must select the appropriate feature packages based on your platform. In short, this trick requires you to sacrifice the portability of applications in exchange for the operational efficiency that can be achieved only through direct programming of the underlying host. Here are some feature packages you can choose to improve efficiency:

Cython

Pylnlne

PyPy

Pyrex

The use of these feature packages varies. For example, using the Data Type in C language can make tasks involving memory operations more efficient or intuitive. Pyrex can help Python extend this feature. Pylnline enables you to directly use C code in Python applications. Inline code is compiled independently, but it stores all the compiled files somewhere and can fully utilize the efficiency provided by C.

Tip 2: Use keys for sorting

Python contains many old sorting rules that take a lot of time when you create a custom sorting method, and these sorting methods also delay the actual running speed of the program. The best sorting method is to use as many keys as possible and the built-in sort () method. For example, the following code is used:

In each example, the list is sorted Based on the index you selected as the key parameter. This method is not only valid for the numeric type, but also applicable to the string type.

Tip 3: loop Optimization

Each programming language emphasizes an optimized loop solution. When using Python, you can use a variety of techniques to make the loop program run faster. However, developers often forget to try to avoid accessing the attributes of variables in a loop. For example, the following code is used:

Every time you call str. upper, Python calculates the subvalue. However, if you assign this evaluate value to a variable, the evaluate result will be known in advance and the Python program will be able to run faster. Therefore, the key is to minimize the workload of Python in the loop. Because of the Python interpretation and execution features, the above example will greatly slow down its speed.

(Note: there are still many methods to optimize the cycle, which is only one of them. For example, many Programmers think that the List derivation is the best way to speed up the loop. The key is that the optimization loop solution is a good choice for improving the speed of application running .)

Tip 4: Use a newer Python version

If you search for Python on the internet, you will find countless information about how to upgrade Python. In general, each version of Python contains optimized content so that it runs faster than the previous version. However, the limiting factor is whether your favorite function library supports the new Python version for Synchronous updates. The key to discussing whether the function library should be updated is whether the new Python version is efficient enough to support this update.

Make sure that your code runs in the new version. You need to use the new function library to experience the new Python version, and then you need to check your applications when making key changes. Only after you complete the necessary corrections can you understand the differences between the new versions.

However, if you just make sure that your application can run in the new version, you may miss the new features provided by the new version. Once you decide to update your application, analyze the performance of your application in the new version, check the parts that may have problems, and then prioritize the features of the new version. Only in this way can users perceive the improvement of application performance at the beginning of the update.

Tip 5: Try multiple encoding methods

Using the same encoding method each time an application is created will result in unsatisfactory running efficiency. Some experimental methods can be attempted during program analysis. For example, when processing data items in a dictionary, you can use a safe method to ensure that the data items already exist before updating them. You can also directly update the data items, separate nonexistent data items as special cases. See the first code below:

When myDict is empty at the beginning, this code runs faster. However, in general, myDict fills up with at least a majority of data, and another method is more efficient.

The output results are the same in both methods. The difference is how the output is obtained. Jump out of the conventional mindset and create new programming skills to make your applications more efficient.

Tip 6: Cross-compile your application

Developers sometimes forget that computers do not actually understand the programming language used to create modern applications. Computers understand machine languages. To run your application, you can use an application to convert your compiled human-readable code into machine-readable code. Sometimes, it is feasible for you to write an application in a language such as Python and run your application in a language such as C ++. The key is what you want your application to do, and what resources your host system can provide.

Nuitka is an interesting cross-compiler that converts your Python code into C ++ code. In this way, you can execute your own applications in native mode without relying on the interpreter program. You will find that the running efficiency of your applications has been greatly improved, but this may vary depending on the platform and task.

(Note: Nuitka is still in the testing stage, so pay attention to it in practical applications. In fact, it is best to use it for experiments now. In addition, there is still room for discussion about whether cross-compilation is the best way to improve the running efficiency. Developers have been using cross-compilation for many years to speed up applications. Remember, each solution has advantages and disadvantages. Please weigh it carefully before applying it to the production environment .)

Make sure that the cross compiler supports your Python version. Nuitka supports Python2.6, 2.7, 3.2, and 3.3. To make the solution 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-compilation may cause some serious problems. For example, when using Nuitka, you will find that even a small program will consume a huge amount of drive space. Because Nuitka uses a series of Dynamic Link Libraries (DDLs) to execute Python functions. Therefore, if you are using a system with limited resources, this method may not be feasible.

Conclusion

The six tips mentioned above can help you create a more efficient Python application. But Silver bullet does not exist. These tips may not always work. In a specific Python version, some tips may be better than others, but sometimes it may even depend on the platform differences. You need to summarize and analyze your application, find its inefficiency, and then try these tips to find the best solution to the problem.

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.