10 cases that make Python execution speed increase rapidly

1. Clever use of Python data types

In a Python program, when the operation for i in list1 is performed on a list, the time complexity is O(n), and when the operation for i in set1 is performed on a set, the time complexity is O(1). Therefore, there will be a very large gap in the execution time of the program.

2. Use iterators instead of lists

Using iterators to replace lists has gradually become a method used by Python programmers. Using iterators can not only save time, but more importantly, save a lot of memory space. The operation takes about 8M of memory, but the iterator takes only 88 bytes.

3. Use local variables instead of global variables 

In the above program, the global variable z is put into the function myFunc, and the execution time of local variables is shorter than that of global variables, so the execution time of the program is also greatly shortened.

4. Avoid "dot" operations

As you can see in the figure above, the only difference between the calculateSqrtWithDot function and the calculateSqrt function is whether the sqrt function is called through a dot operation. Whenever we point an operation to call a function, specific methods will be triggered, for example, getattribute () and getattr (). Such methods will call dictionary operations, which will consume time, so when the program calls third-party libraries , you can try to use the method from xx import xx to call.

5. Avoid unnecessary class abstractions

In the class, try not to use decorators, descriptors and other operations to wrap the program, which will bring a burden to the operation of the program, so if it is not necessary, do not wrap the program in this way.

6. Avoid meaningless data copies

In the program above, list6 is a meaningless copy of data, which not only causes the loss of program running time, but also wastes memory resources.

7. Avoid using temporary variables when changing values

In the above program, the temporary variable of temp is not needed, and the use of the temporary variable temp increases the running time of the program.

8. Operation of String Variables

When the string str1 and str2 operations use the "+" sign, the Python interpreter will apply for memory space and copy the data str1 and str2 into the new memory space respectively, so when performing N times of string "+" operations , N-1 intermediate results will be generated, and each intermediate result will be copied into a new memory space.

When using the join function, the join function will calculate all the required memory space at one time, then allocate the memory space, and copy all the string elements into the allocated memory.

9. Use if to judge

When using if to make judgments, there are two commonly used methods. 1 if...and.... 2 if...or.... In order to save the operation time of the program, when judging if x and y, x needs to be a judgment condition with a relatively high possibility of False. When judging if x or y, x needs to be a judgment condition with a relatively high possibility of True.

10. Use for loops instead of while loops

In the program above, you can see that the same function uses the for loop to replace the while loop, and the for loop is faster than the while loop.

Summarize

Through the above 10 small cases, we did not use any third-party libraries or decorators, and simply improved the running speed of Python programs from the perspective of program optimization. You can learn from the above ten small cases to improve the running speed of the program in the daily program writing process.

In the end, the editor will also present a python spree to everyone [Jiajun Yang: 419693945] to help everyone learn better!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324341416&siteId=291194637