7 habits to improve Python performance

Reprinted from: https://zhuanlan.zhihu.com/p/38160586

1. Use local variables

Try to use local variables instead of global variables: easy to maintain, improve performance and save memory.

Use local variables to replace variables in the module namespace, for example, ls = os.linesep. On the one hand, the program performance can be improved, and local variable search speed can be faster; on the other hand, short identifiers can be used to replace lengthy module variables to improve readability.

2. Reduce the number of function calls

When judging the object type, the isinstance() is the best, the object type identity (id()) is the second, and the object value (type()) is the second most.

#判断变量num是否为整数类型type(num) == type(0) #调用三次函数type(num) is type(0) #身份比较isinstance(num,(int)) #调用一次函数

Don't put the content of repeated operations as parameters in the loop condition to avoid repeated operations.

#每次循环都需要重新执行len(a)while i < len(a):    statement#len(a)仅执行一次m = len(a)while i < m:    statement

If you want to use a function or object Y in module X, you should directly use from X import Y instead of import X; XY. In this way, when Y is used, one query can be reduced (the interpreter does not need to find the X module first, and then find Y in the dictionary of the X module).

3. Use mapping instead of search conditions

The search speed of mapping (such as dict, etc.) is much faster than that of conditional statements (such as if, etc.). There is no select-case statement in Python.

#if查找if a == 1:    b = 10elif a == 2:    b = 20...#dict查找,性能更优d = {1:10,2:20,...}b = d[a]

4. Iterate over sequence elements directly

For sequences (str, list, tuple, etc.), iterating the sequence elements directly is faster than iterating the index of the elements.

a = [1,2,3]#迭代元素for item in a:    print(item)#迭代索引for i in range(len(a)):   print(a[i])

5. Use generator expressions instead of list comprehensions

List comprehension (list comprehension), will produce the entire list, the iteration of large amounts of data will have a negative effect.

The generator expression does not. It does not actually create a list, but returns a generator to generate a value when needed (delayed calculation), which is more memory friendly.

#计算文件f的非空字符个数#生成器表达式l = sum([len(word) for line in f for word in line.split()])#列表解析l = sum(len(word) for line in f for word in line.split())

6. First compile and then call

When using the eval() and exec() functions to execute code, it is better to call the code object (compiled into bytecode through the compile() function in advance) instead of calling str directly, which can avoid multiple executions of repeated compilation processes and improve program performance .

Regular expression pattern matching is similar. It is also best to compile the regular expression pattern into a regex object (via the re.complie() function), and then perform comparison and matching.

7. Modular programming habits

The highest-level Python statements in a module (code without indentation) are executed when the module is imported (regardless of whether it is really necessary to execute). Therefore, you should try to put all the function codes of the module in the function, including the function code related to the main program can also be put in the main () function, the main program itself calls the main () function.

You can write test code in the main() function of the module. In the main program, check the value of name, if it is'main' (indicating that the module is executed directly), call the main() function to test; if it is the module name (indicating that the module is called), then do not test .

I recommend my original " PyCharm Chinese Guide " e-book, which contains a large number (300) of illustrations . It is well-made and worthy of a collection by every Python engineer.

The address is: http://pycharm.iswbm.com

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/109008352