I translated from "Exper Python Programming"
' Premature optimization is the root of all evil in programming '-donald Knuth
Three Principles of optimization
One of the most common mistakes is that we begin to optimize the code at the beginning of the code. The sad thing is that this is usually done useless work, a lot of software only you run up to find its real bottleneck where.
- Look at the problem from the user's point of view
There was a team that ran faster for their projects and eventually reached their satisfaction, eventually delivering it to the customer, and found that the customer didn't think it would be good for him to run fast ...
- Always keep code readable (maintainable)
If you have completed 90% of the optimization tasks, while the remaining 10% of the optimization tasks may make your code completely unreadable, then you might want to stop your optimization work.
Optimized strategy
- To find someone else's problem.
In general, our software testing is not possible to achieve 100% simulation of the real environment. When a customer complains to us that the software is not running as fast as before, we might as well look for the speed problems that other applications have caused when interacting with other applications.
This people are understood, running slowly? First see if you are not configured enough Ah, enough to buy buy buy!
- Write a Speed test document
When we start to optimize our work, we might as well write a comment under our objective function "the function cannot run longer than 1 seconds"
Looking for bottlenecks
From the perspective of the CPU, it is divided into macro-search and microscopic search. The most common here is cprofile, a C-language library that provides a record of the time it takes to monitor the use of program resources. The specific use method can point here.
Since we're talking about python, we're going to have to mention Python's memory allocation mechanism, and unlike the C language, we can use a function to know the memory allocation of the current variable, and in Python we never know how much memory space a variable is currently allocating. Typically, memory space is consumed for the following reasons: 1. An uncontrolled growth of a variable; 2. There are too many instances in the global, and they do not monitor their use of memory; 3. The thread that does not shut down properly; 4. Objects with __del__ properties in a loop body
The specific memory detection tools are: Guppy, heapy
This part of the story we use the Universal Network monitoring tools just fine.
reduce the complexity of your code
Two dimensions:
- Measure Branch Complexity: How many branches of a ifelse statement in a program
- Measurement of time space complexity
The last two major themes are: multithreading and caching. These two methods are also very important to the optimizer method, first written here, read on to continue more.
Python Note-python programming Optimization: Common principles and technical introduction