Python is a very cool language, because very few Python code can do a lot of things in a short time, and Python can easily support multitasking and multiprocessing.
Py
1, the key code can rely on the expansion package
Python makes many programming tasks simpler, but does not always provide the best performance for very critical tasks. Performing critical tasks with C, C + +, or machine language expansion packs can greatly improve performance. These packages are platform-dependent, that is, you must use a specific package that is relevant to the platform you are using. In short, the solution provides some portability of applications in exchange for performance that you can obtain only by programming directly to the underlying host. The following expansion packs you can consider adding to your personal extension library:
Cython
Pyinlne
PyPy
Pyrex
These packages have different roles and methods of execution. For example, Pyrex makes it easy and efficient for Python to handle some memory tasks, Pyinline can directly let you use C code in a Python application, although inline code is compiled separately, but if you can use C code efficiently, it can handle everything in the same place.
2, the use of keyword sorting
There are a lot of old Python code that will take extra time to create a custom sort function when it is executed. The best way to sort it is to use the keyword and the default sort () method to see the following example:
Copy Code code as follows:
Import operator
Somelist = [(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)],
The list of each case is sorted according to the index you chose as the keyword parameter, which is also true for string and numeric sorting.
3, optimize the cycle
Every programming language emphasizes the optimization of circular statements, as is Python. Although you can rely on rich technology to make the loop run faster, one way developers often ignore it is to avoid using dot concatenation strings within loops. For the following example:
Copy Code code as follows:
Lowerlist = [' This ', ' is ', ' lowercase ']
Upper = Str.upper
Upperlist = []
Append = Upperlist.append
For word in lowerlist:
Append (upper (word))
Print (upperlist)
#Output = [' This ', ' is ', ' lowercase ']
Every time the call Str.upper,python the value of the method. But if you put the result of the evaluation into a variable, you can improve the performance of the program. The key is to reduce the number of loops that are executed in Python because it is slow to parse these instances by Python.
4. Use the new version
Anyone who searches for Python data online will find countless information about the Python version migration. Typically, each version of Python is optimized and improved for a previous version to make Python run faster. The limiting factor is whether your favorite library of functions has also been improved for the new version of Python.
When you use a new library of functions and get a new version of Python, you need to ensure that the code is still operational, check the application, and fix the differences.
Then, if you just guarantee that the application will be able to run on the new version, you may miss the new feature update. Once you have made improvements, configure the application under the new version, check the problem area and prioritize new feature updates, and the user will see a greater performance boost for previous upgrades.
5. Try a variety of programming methods
Every time you create an application, you use the same programming method, and in some cases the program runs slower than expected. Do some small experiments in the process of analysis. For example, when managing a data item in a dictionary, you can use a secure method to determine whether a data item already exists and need to update it, or you can add entries directly and then handle situations where the project does not exist at all.
Copy Code code as follows:
n = 16
Mydict = {}
For I in range (0, N):
char = ' ABCD ' [i%4]
If char not in mydict:
Mydict[char] = 0
Mydict[char] + + 1
Print (mydict)
When Mydict is empty, the above code usually runs faster. But when mydict already has data padding, there is a better way to choose:
Copy Code code as follows:
n = 16
Mydict = {}
For I in range (0, N):
char = ' ABCD ' [i%4]
Try
Mydict[char] + + 1
Except Keyerror:
Mydict[char] = 1
Print (mydict)
In both cases output {' d ': 4, ' C ': 4, ' B ': 4, ' A ': 4}, the only difference is how the output is obtained. Standing outside the box to consider and create new programming techniques will allow your program to get faster speed.
6. Cross-Compile program
Developers sometimes forget that the computer does not recognize any of the current application language, it only recognizes machine code. In order to run the program, an application is needed to convert human-readable code into code that the computer can recognize. When you write a program in one language, such as Python, and then run it in a different language, such as C + +, it makes sense from a performance standpoint. This depends on what you want to do with this application and what resources the host system can provide.
An interesting crossover compiler, Nuitka, converts Python into C + + code, and the result is that you can execute the application in native mode instead of relying on the interpreter. Depending on the platform and the tasks, you can see significant performance improvements.
The above is the full content of this article, I hope to learn Python help.