[Four python Python applications to enhance operational performance of] 7 Habits

We all know that RPA arts tournament flag depends on the python language.
So we can have some skills, can maximize performance Python program, also avoid unnecessary waste of resources.
1, using local variables

 

Make use of a local variable instead of the global variables: easy to maintain, improve performance and save memory.

 

Local variable substitution module namespace variables, e.g. ls = os.linesep. On the one hand can improve program performance, look for local variables faster; on the other hand the available alternatives lengthy brief identifier module variables, improve readability.
2, reduce the number of function calls

 

When the object type determination using isinstance () optimal use of the type of object identity (id ()), followed by the use of object value (type ()) compare most times.

 

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

 

Do not repeat the content as a parameter into the loop condition, to avoid repetitive operations.

 

1.  `#每次循环都需要重新执行len(a)`

2. `while i < len(a):`

3. `   statement`

4.

5. `#len(a)仅执行一次`

6. `m = len(a)`

7. `while i < m:`

8. `   statement`

 

To use a function or object in the module X Y, should be used directly from X import Y, rather than import X; XY. So that when using the Y, you can reduce a query (interpreter module without having to first find X, Y and then look in the dictionary X module).
3, using the map to find alternative conditions

 

Mapping (such as dict etc.) much faster than the search conditional statements (e.g., if the like). Python is also no select-case statement.

 

1.  `#if查找`

2. `if a == 1:`

3. `b = 10`

4. `elif a == 2:`

5. `b = 20`

6. `...`

7.

8. `#dict查找,性能更优`

9. `d = {1:10,2:20,...}`

10. `b = d[a]`

 

4, direct iterative sequence element

 

Sequence (str, list, tuple, etc.), direct iterative sequence elements, interior elements than the index rate to be faster.

 

1.  `a = [1,2,3]`

2.

3. `#迭代元素`

4. `for item in a:`

5. `   print(item)`

6.

7. `#迭代索引`

8. `for i in range(len(a)):`

9. ` print(a[i])`

 

5, using a list of alternative analytical generator expression

 

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

 

The generator expression is not, it does not really create a list, but returns a generator, produces a value (delay calculation) if necessary, more memory-friendly.

 

1.  `#计算文件f的非空字符个数`

2. `#生成器表达式`

3. `l = sum([len(word) for line in f for word in line.split()])`

4.

5. `#列表解析`

6. `l = sum(len(word) for line in f for word in line.split())`

6, compiled after the first call

When using the eval (), exec () function to execute code, preferably the calling code object (advanced through compile () function is compiled into byte code), instead of calling str, avoid performing the compilation process is repeated several times, to improve program performance .

Regular expression pattern matching is similar, but also the best first regular expression pattern compiler to regex object (by re.complie () function), and then performs comparison and matching.
7, module programming practice

Execution (regardless of whether it is necessary to perform) when the module is the highest level of Python statements (not indented code) will import module (import). Thus, all the module should function code into function, including the main program code can also be related functionality into the main () function, the main program itself calls main () function.

() Function to test the writing of code in the main module. In the main program, the name of the value detected, if 'main' (a block is executed directly), is called main () function, a test; if a module name (a block is called), not tested .

Guess you like

Origin www.cnblogs.com/valorchang/p/11357405.html