Principle 1: Not optimized
Principle 2: Do not optimize those unimportant parts (otherwise it will decrease readability)
Solution:
1. Using functions, local variables are much faster than global variables. Use functions as much as possible, such as main ()
2. Selective removal of property access. If you use the From math import sqrt instead of calling math.sqrt () more than once in the program, or declaring the local variable directly.
Import Math def compute_roots (nums): = math.sqrt = [] = res.append for in nums: res_append (sqrt (n)) return Res
# Micro-Optimization
A = {i:i**2 for I in range (1000)} # Faster
b = [Dict (i=i**2) for I in range (1000)] # slower
3. Avoid unnecessary abstractions, such as adorners, @property, etc.
4. Use built-in strings, tuples, lists, collections, dictionaries and other containers.
5. Avoid unnecessary data structures or copy actions
6. Use CPython or PyPy and so on.
Ps: >> optimization before the first to run up, first to have the correct algorithm! Algorithms with low complexity are far more important than program optimization.
How to make Python programs run faster