Six Methods to Improve Python program running efficiency, python program running
Python is a cool language, because you can use a small amount of code to do a lot of things in a short time. In addition, it can easily support multiple tasks, such as multiple processes. Python critics sometimes say that Python execution is slow. This article will introduce 6 tips to accelerate your Python application.
1. Make key code dependent on external packages
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 to improve application performance. These packages cannot be cross-platform, which means you need to find a suitable package based on the platform you are using. In short, this solution abandons the portability of some applications in exchange for program performance that can be achieved only through direct programming on a specific host. Here are some packages you should consider adding to your "performance engineering factory:
- Cython
- PyInlne
- PyPy
- Pyrex
These packages improve performance in different ways. For example, Pyrex can extend what Python can do, such as using the Data Type of C to make memory tasks more effective or direct. PyInIne allows you to directly use C code in Python applications. The Inline code in the program is compiled separately, but it makes all the code in the same place while taking advantage of the efficiency provided by the C language.
2. Use the key for sorting)
There are a lot of old Python sorting code, which takes you time to create a custom sorting, but it does accelerate the execution of the sorting process at runtime. The best way to sort elements is to try to use the key and the default sort () sorting method. 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)]somelist.sort(key=operator.itemgetter(2))somelist#Output = [(6, 2, 4), (9, 7, 5), (1, 5, 8)],
In each instance, the array is sorted Based on the index you selected as the key parameter. Similar to sorting by numbers, this method is also applicable to sorting by strings.
3. Optimized cycle
Each programming language emphasizes the need to optimize the cycle. When using Python, you can rely on a lot of techniques to make the loop run faster. However, one method that developers often miss is to avoid using point operations in a loop. For example, consider the following code:
lowerlist = ['this', 'is', 'lowercase']upper = str.upperupperlist = []append = upperlist.appendfor word in lowerlist: append(upper(word)) print(upperlist) #Output = ['THIS', 'IS', 'LOWERCASE']
Every time you call the str. upper method, Python will evaluate the value of this method. However, if you replace the obtained value with a variable, the value becomes known, and Python can execute the task faster. The key to optimizing the loop is to reduce the workload of running Python in the loop, because the Python native interpreter will actually slow down the execution speed in that case.
(Note: There are many methods to optimize the cycle, which is only one of them. For example, many programmers say that list derivation is the best way to speed up execution in a loop. The key here is that optimizing the loop is one of the better ways for programs to achieve higher execution speeds .)
4. Use a newer version of Python
When you search for Python information online, you will find numerous questions about the migration from one Python version to another. In general, each version of Python contains optimizations that allow it to run faster than the previous version. The limit for version migration is whether your favorite libraries have been migrated to a newer version of Python. Compared with querying whether version migration should be performed, the key issue is to determine when a new version has sufficient support to ensure the feasibility of migration.
You need to verify that your code is still running. You need to use the new library you obtained in the new version of Python, and then check whether your application needs major changes. Only after you make the necessary corrections will you notice the differences between versions. However, if you make sure that your application runs in the new version without any changes, you may miss the new features of those version updates. Once you perform the migration, you should write a description for the application in the new version, check the problematic areas, and prioritize the use of the features of the new version to update those areas. In this way, you will see a larger performance improvement earlier during the upgrade process.
5. Try multiple encoding methods
If you use the same encoding method every time you create an application, it will almost certainly lead to situations where your application is slower than it can achieve. As part of the analysis process, you can try some experiments. For example, to manage some elements in a dictionary, you can use a safe method to determine whether the element already exists and is updated, or you can add the element directly, and then handle the Nonexistent element as an exception. Consider the first encoding example:
n = 16myDict = {}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 myDict starts to be empty. However, when mydict is typically filled with data (or at least most of the data), another method works better.
n = 16myDict = {}for i in range(0, n): char = 'abcd'[i%4] try: myDict[char] += 1 except KeyError: myDict[char] = 1 print(myDict)
In both cases, the output has the same: {'D': 4, 'C': 4, 'B': 4, 'A': 4 }. The only difference is how the output is obtained. Jump out of a fixed mindset and create new coding skills to help you use your applications to get faster results.
6. Cross-compile the application
Developers sometimes forget that computers do not understand any language used to create modern applications, and computers can understand machine code. To run applications on your computer, you can use an application to convert human-readable code into something that your computer can understand. Sometimes it makes sense to write an application in one language, such as Python, and run it in another language, such as C ++. It depends on what you want the application to do and the resources that the host system can provide.
An interesting cross-compiler, Nuitka, can convert your Python code into C ++ code. The result is that you can execute an application in the native mode, instead of relying on the interpreter. Based on the platform and task, you can see a significant performance improvement.
(Note: Nuitka is still in the testing stage, so be careful when using it for product programs. In fact, it is best to use it for experiments now. 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 better application speed. Remember, every solution has its own advantages and disadvantages. You should consider the gains and losses before applying a solution to the production environment .)
When using a cross compiler, make sure that it supports your Python version. Nuitka supports Python2.6, 2.7, 3.2, and 3.3. To make this solution work, you need a Python interpreter and a C ++ compiler. Nuitka supports multiple C ++ compilers, including Microsoft Visual Studio, MinGW, and Clang/LLVM.
Cross-compilation may also have some serious negative effects. For example, when using Nuitka to work, you will find that even a small program can consume a lot of hard disk space, because Nuitka uses a large number of dynamic link libraries (DLLs) to implement Python functions. So when you are facing a system with limited resources, this solution may not work very well.
Summary
Any of these six tips can help you create faster Python programs. However, no skill is omnipotent and cannot work every time. Some tips are more effective than others in a specific version of Python-even the system platform can affect their performance. You need to configure your application, determine where to make it run slowly, and then try some tips that seem best to solve these problems.