6 ways to improve the efficiency of a Python program

Source: Internet
Author: User
Python is a cool language because you can do a lot of things with very little code in a very short period of time. Not only that, it can also easily support multitasking, such as multi-process. Python critics sometimes say python execution is slow. This article will try to introduce 6 tips to speed up your Python application.
1. Make the critical code dependent on the external package

Although Python makes many programming tasks easy, it may not always provide the best performance for urgent tasks. You can use an external package written in C, C + +, or machine language for urgent tasks, which can improve the performance of your application. These packages are not cross-platform, which means you need to find the right package based on the platform you are using. In short, this scenario abandons some application portability in exchange for program performance that can only be programmed directly on a specific host. Here are some of the packages you should consider adding to your "performance Arsenal":

    • Cython
    • Pyinlne
    • PyPy
    • Pyrex

These packages improve performance in different ways. For example, Pyrex can extend what Python can do, such as using C's data type to make memory tasks more efficient or straightforward. Pyinine lets you use C code directly in a python application. The inline code in the program compiles separately, but it also allows all the code to be in the same place while taking advantage of the efficiency provided by the C language.
2. Use the key when sorting (key)

There are a lot of old Python sort codes that take your time when you create a custom sort, but you can actually speed up the sort process at run time. The best way to sort elements is to use the key (key) and the default sort () method whenever possible. For example, consider the following code:

Import Operatorsomelist = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]somelist.sort (key=operator.itemgetter (0)) Somelist#output = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]somelist.sort (Key=operator.itemgetter (1)) Somelist#output = [(6, 2, 4), (1, 5, 8), (9, 7, 5)]s Omelist.sort (Key=operator.itemgetter (2)) Somelist#output = [(6, 2, 4), (9, 7, 5), (1, 5, 8)],

In each instance, the array is sorted according to the index you selected as the key parameter part. Similar to the use of numbers for sorting, this method is also useful for sorting by string.
3. Optimize loops

Each programming language will emphasize the need to optimize loops. When using Python, you can rely on a lot of tricks to make loops run faster. However, one way developers often miss out is to avoid using point operations in a loop. For example, consider the following code:

Lowerlist = [' This ', ' was ', ' lowercase ']upper = str.upperupperlist = []append = Upperlist.appendfor word in lowerlist:  Append (upper (word))  print (upperlist)  #Output = [' This ', ' is ', ' lowercase ']

Every time you call a method, Str.upper,python will ask for the value of the method. However, if you use a variable instead of the evaluated value, the value becomes known, and Python can perform the task more quickly. The key to optimizing the loop is to reduce the amount of work that Python performs inside the loop, because the Python native interpreter really slows down execution in that case.

(Note: There are many ways to optimize loops, which is just one of them.) For example, many programmers would say that list deduction is the best way to improve execution speed in a loop. The key here is that the optimization cycle is one of the better ways for a program to achieve higher execution speed. )
4. Using a newer version of Python

Searching for Python information on the web will find countless people asking questions about migrating from one version of Python to another. In general, each version of Python contains optimizations that allow it to run faster than the previous version. The limiting factor for version migration is whether the libraries you prefer have been migrated to a newer version of Python. Rather than asking whether a version migration should be done, the key issue is determining when a new version will have enough support to ensure the viability of the migration.

You need to verify that your code is still running. You will need to use the new library you obtained in the new version of Python, and then check if your application requires significant changes. Only after you make the necessary corrections will you notice the differences between versions. However, if you happen to make sure that your application will run in the new version without any changes, you may miss out on the new features that the version upgrade brings. Once you have migrated, you should write a description for the application under your new version, check the problematic areas, and prioritize the new versions to update those places. This way, users will see a greater performance boost earlier in the upgrade process.
5. Try multiple coding methods

If every time you create an application that is using the same encoding method, it will almost certainly cause some of your applications to run slower than it can achieve. As part of the analysis process, you can try some experiments. For example, to manage elements in a dictionary, you can use a secure method to determine whether an element is already present and updated, or you can add the element directly, and then handle the element as an exception that does not exist. Consider an example of the first encoding:

n = 16myDict = {}for i in range (0, N):  char = ' ABCD ' [i%4]  if char not in mydict:    Mydict[char] = 0    mydict[c HAR] + = 1    print (mydict)

This code usually runs faster when the mydict begins to empty. However, when mydict is usually filled with data (or at least most of it is filled), the other method works better.

n = 16myDict = {}for i in range (0, N):  char = ' ABCD ' [i%4]  try:    Mydict[char] + = 1  except Keyerror:    Mydi Ct[char] = 1  print (mydict)

Both cases have the same output: {' d ': 4, ' C ': 4, ' B ': 4, ' A ': 4}. The only difference is how this output is obtained. Jump out of a fixed mindset and create new coding skills that will help you get faster results with your application.
6. Cross-Compiling the application

Developers sometimes forget that computers actually do not understand any language used to create modern applications, and computers can understand machine code. To be able to run the application on a computer, you use an app to translate human-readable code into a computer that you can understand. Sometimes it is meaningful to write an application in one language, such as Python, and to run it in another language, such as C + +, from a performance standpoint. It depends on what you want the application to do, as well as the resources that the host system can provide.

An interesting cross compiler, Nuitka, can convert your Python code to C + + code. As a result, you can execute the application in native mode instead of relying on the interpreter. Depending on the platform and the task, you can see a significant performance boost.

(Note: Nuitka is still in beta, so be careful when using it for product programs.) In fact, it is best to use it for experimentation. There are also some discussions about whether cross-compilation is the best way to get better performance. Developers have been using cross-compiling for years to achieve specific goals, such as the speed of a better application. Remember, every solution will be gains, and you should consider the pros and cons before you use a solution for your production environment. )

When using a cross-compiler, make sure that it supports the version of Python that you use. The Nuitka supports Python2.6, 2.7, 3.2, and 3.3. For this scenario to work, you need a Python interpreter and a C + + compiler. Nuitka supports a variety of C + + compilers, including Microsoft Visual Studio, MinGW, and CLANG/LLVM.

Cross-compiling can also have some serious negative effects. For example, when working with Nuitka, you'll find that even a small program consumes a lot of hard disk space, because Nuitka uses a large number of dynamic-link libraries (DLLs) to implement Python functionality. So when you're faced with a system with limited resources, this scenario might not work well.
Summarize

Any of these six tips can help you create a faster Python program. But any skill is not omnipotent, can not work every time. Some techniques are more effective in Python specific versions than other tricks-even the system platform can affect their effectiveness. You need to configure your app, determine where to run it slowly, and then try some of the tricks that seem to best solve these problems.

  • Related Article

    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.