6 ways to improve the efficiency of Python programs _python

Source: Internet
Author: User
Tags lowercase

Python is a cool language because you can do a lot of things with very little code in a very short time. Not only that, it can also easily support multitasking, such as multiple processes. Python critics sometimes say that Python executes slowly. This article will try to introduce 6 tips to speed up your Python application.
1. Make critical code dependent on the external package

While Python makes many programming tasks easier, it may not always provide the best performance for urgent tasks. You can use external packages 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 applications for program performance that can only be obtained 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 a memory task 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 makes all the code in the same place while taking advantage of the efficiency that C can provide.
2. Use keys (key) when sorting

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

Import operator
somelist = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]
somelist.sort (key=operator.itemgetter (0))
Someli St
#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)]
Somelist.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 part of the key parameter. Similar to using numbers for sorting, this method also works with string sorting.
3. Optimization Cycle

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

Lowerlist = [' This ', ' is ', ' lowercase ']
upper = Str.upper
upperlist = []
append = Upperlist.append
For word in Lowerlist:
  append (upper (word))
  print (upperlist)
  #Output = [' Here ', ' is ', ' lowercase ']

Every time you call a method Str.upper,python the value of the method is evaluated. 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 cycle is to reduce the amount of work that Python does within the loop, because the Python native interpreter will really slow down execution in that case.

(Note: There are a lot of ways to optimize loops, and this is just one of them.) For example, many programmers would say that list derivation is the best way to increase execution speed in a loop. The key here is that optimizing loops is one of the better ways that programs get higher execution speed. )
4. Using a newer version of Python

Searching the web for Python information 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 your favorite libraries have migrated to a newer version of Python. The key issue is to determine when a new version has sufficient support to ensure that the migration is feasible, rather than asking if a version migration should be made.

You need to verify that your code is still running. You'll need to use the new library you've acquired under the new Python version, and then check if your application requires significant changes. Only after you make the necessary corrections will you notice the difference between the versions. However, if you are just making sure that your application runs under the new version without any changes, you may be missing out on the new features that the version upgrades bring. Once you have migrated, you should write a description for your new version of the application, check for problems, and give priority to using the features of the new version to update those places. This allows the user to see a greater performance improvement earlier in the upgrade process.
5. Try multiple coding methods

If every time you create an application that uses the same coding method, it will almost certainly cause some of your applications to run more slowly 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 safe method to determine if an element is already present and updated, or you can add an element directly, and then handle the element as an exception. Consider an example of the first encoding:

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

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

n =
mydict = {}
for I in range (0, N):
  char = ' ABCD ' [i%4]
  try:
    Mydict[char] + = 1
  except Ke Yerror:
    Mydict[char] = 1
  print (mydict)

Two cases have the same output: {' d ': 4, ' C ': 4, ' B ': 4, ' A ': 4}. The only difference is how this output is obtained. Stepping out of a fixed mindset and creating new coding techniques can help you get faster results with your application.
6. Cross-Compile the application

Developers sometimes forget that computers actually don't understand any language used to create modern applications, and computers can understand machine code. In order to be able to run applications on a computer, you use an application that converts human-readable code into a computer that you can understand. Sometimes it's useful to use a language, such as Python, to write an application and run it in another language, such as C + +, from a performance point of view. It depends on what you want the application to do, and the resources the host system can provide.

An interesting crossover compiler, Nuitka, can convert your Python code into C + + code. The result is that 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 to product programs.) In fact, it is best to use it for experimentation at the moment. There are also some discussions about whether cross compilation is the best way to get better performance. Developers have been using cross compilation for several years to achieve specific goals, such as the speed of better applications. Remember, every solution will be gain, and you should consider the pros and cons before you use a solution in a production environment. )

When using a cross compiler, make sure it supports the version of Python you are using. Nuitka supports Python2.6, 2.7, 3.2, and 3.3. To make this scenario 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-compilation can also have some serious negative effects. For example, when working with Nuitka, you'll find that even a small program can consume a lot of hard disk space because Nuitka uses a lot of dynamic link libraries (DLLs) to implement Python's functionality. So when you're faced with a system with limited resources, the solution may not work well.
Summarize

Any of these six tips can help you create faster Python programs. But any skill is not a panacea, and cannot work every time. Some techniques are more effective in specific versions of Python than others-even the system platform can affect their effects. You need to configure your application to determine where to run it slowly, and then try some of the techniques that seem to be the best solution to 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.