- Using a generator can save a lot of memory.
- When concatenating strings,+ is not used+ Memory will first create a new string, then concatenate the two old strings and copy them to the new string. Using format,% s, join;
- Loop code optimization. Reduce the number of loops, reduce time complexity, and avoid excessive execution of duplicate code. Put the ones that can be directly judged and have fewer iterations before execution.
- Use built-in functions more frequently. Like set, dict. The complexity of finding elements is only 1.
- Multi process, multi threading, coroutine.
- Multiple if else condition judgments can be made by placing the most likely conditions that occur first, which can reduce the number of judgments.
- Multiple assignments are often used. Do not use temporary variables. For example:
temp = a
a = b
b = temp
#change to
a,b=b,a
8. Try to use local variables as much as possible. Retrieve local variables not only faster than global variables. Moreover, the global variable can easily affect the readability of the code.