Although Python programs are simple to write and easy to use, compared with C++, Java and other programming languages, Python programs have no advantage in running speed.

There are many third-party libraries that can help speed up Python, but they are cumbersome to use. Today, xiaobian wants to improve the running speed of the program from the point of view of programming.

1. Make clever use of Python data types

In Python, for I in list1 on a list is O(n), and for I in set1 on a set is O(1). So in the execution time of the program, there will be a very large gap.

Use iterators instead of lists

Use iterator instead of a list, has gradually become the Python programmers use a method, using the iterator operations, not only can save time, more important is can save a lot of memory space, above, using the list of operation takes up about 8 MB of memory, but the iterator 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. The execution time of the local variable is shorter than that of the global variable, so the execution time of the program is greatly shortened.

4. Avoid point-to-point operations

As you can see in the figure above, the only difference between the calculateSqrtWithDot function and the calculateSqrt function is whether or not the SQRT function is called through dot operations. Whenever we call a function by clicking on an operation, certain methods, such as getAttribute () and getattr(), are triggered. These methods invoke dictionary operations and are time consuming, so when the program calls a third-party library, Use the method from xx import xx whenever possible.

5. Avoid unnecessary class abstractions

Try not to wrap your program with decorators, descriptors, and so on in a class. This is a burden, so don’t wrap your program if you don’t have to.

6. Avoid unnecessary data copying

In the example above, list6 is a meaningless copy of the data, resulting in a waste of running time and memory resources.

7. Avoid using temporary variables when changing values

In the above program, the temporary variable temp is not needed. Using the temporary variable temp causes the program to run time.

8. String variable manipulation

When the string str1 and str2 operations use the “+” sign, the Python interpreter allocates memory space and copies str1 and str2 into the new memory space, respectively, so N times the string “+” operation produces n-1 intermediate results. And each intermediate result is copied to the new memory space.

When you use the JOIN function, the join function calculates all the required memory space at once, allocates the memory space, and copies all the string elements into the requested memory.

A. if B. if C. if D. if

When using if to judge, there are two ways commonly used. 1 if … and… . 2 if… or … . In order to save the calculation time of the program, when making the judgment of if x and y, x needs to be the judgment condition with high possibility of False. When making the judgment of if x or y, x needs to be the judgment condition with high possibility of True.

Use a for loop instead of a while loop

In the program above, you can see that the same functionality replaces the while loop with a for loop, which is faster than the while loop.

conclusion

Through the above 10 small cases, we did not use any third-party libraries or decorators to improve the speed of Python applications purely from the point of view of application optimization. We can learn from the above ten small cases, in the daily process of programming to improve the speed of the program.