Share some of the code optimization tips you have recently seen.
Code Optimization Part1
Share some of the code optimization tips you have recently seen.
Short-circuit characteristics of if judgment
And should be placed in front of few conditions, so that when a large number of judgments, when few conditions are met, the subsequent expressions will not be computed to save time (because False and True or False)
import timeits1 = """a = range(2000)[i for i in a if i % 2 ==0 and i > 1900]"""s2 = """a = range(2000)[i for i in a if i > 1900 and i % 2 ==0]"""print timeit.timeit(stmt=s1, number=1000)print timeit.timeit(stmt=s2, number=1000)
The running result is as follows:
Export python test6.py0.2485320568080.195827960968 # you can see that the s2 expression is faster to compute because I> 1900 is not satisfied in most cases. in these cases, I % 2 = 0 is not calculated, saving time.
Similarly, for or, more conditions are met before.
import timeits1 = """a = range(2000)[i for i in a if 10 < i <20 or 1000 < i < 2000]"""s2 = """a = range(2000)[i for i in a if 1000 < i < 2000 or 10 < i <20]"""print timeit.timeit(stmt=s1, number=1000)print timeit.timeit(stmt=s2, number=1000)
Running result:
0.2531249523160.202992200851
Join string
Join strings merge faster than Loop use +.
import timeits1 = """a = [str(x) for x in range(2000)]s = ''for i in a: s += i"""s2 = """a = [str(x) for x in range(2000)]s = ''.join(a)"""print timeit.timeit(stmt=s1, number=1000)print timeit.timeit(stmt=s2, number=1000)
The running result is as follows:
python test6.py0.5589458942410.422435998917
While 1 and while True
In python2.x, True and False are not reserved keywords and are global variables, which means you can
>>> True = 0>>> True0>>> if not True:... print '1'...1
So the following two situations:
import timeits1 = """n = 1000000while 1: n -= 1 if n <= 0: break"""s2 = """n = 1000000while True: n -= 1 if n <= 0: break"""print timeit.timeit(stmt=s1, number=100)print timeit.timeit(stmt=s2, number=100)
The running result is as follows:
➜ python test6.py5.180073022846.84624099731
Because each time you judge while True, you must first find the value of True.
In python3.x, True is changed to a keyword parameter, so the above two cases are the same.
CProfile, cStringIO, and cPickle
The C language version is faster than the native version. CPickle vs pickle is as follows:
import timeits1 = """import cPickleimport picklen = range(10000)cPickle.dumps(n)"""s2 = """import cPickleimport picklen = range(10000)pickle.dumps(n)"""print timeit.timeit(stmt=s1, number=100)print timeit.timeit(stmt=s2, number=100)
The running result is as follows:
➜ python test6.py0.1821789741521.70917797089
Rational use of generators
Differences
The result of using () is a generator object. the memory space required is irrelevant to the size of the list, so the efficiency is higher.
import timeits1 = """[i for i in range (100000)]"""s2 = """(i for i in range(100000))"""print timeit.timeit(stmt=s1, number=1000)print timeit.timeit(stmt=s2, number=1000)
Result:
➜ python test6.py5.443274974820.923446893692
However, when loop traversal is required, the efficiency of using the iterator is not high, as shown below:
import timeits1 = """ls = range(1000000)def yield_func(ls): for i in ls: yield i+1for x in yield_func(ls): pass"""s2 = """ls = range(1000000)def not_yield_func(ls): return [i+1 for i in ls]for x in not_yield_func(ls): pass"""print timeit.timeit(stmt=s1, number=10)print timeit.timeit(stmt=s2, number=10)
The result is as follows:
➜ python test6.py1.031867027281.01472687721
Therefore, using the generator is a trade-off result, which comprehensively considers the memory and speed.
Xrange
In python2.x, xrange is a pure C-implemented generator. compared with range, it does not calculate all values in the memory at a time. However, the limitation is that it can only work with integers: you cannot use long or float.
Import statement overhead
The import statements are sometimes used to restrict the scope of their functions or reduce the initialization time, but the python interpreter does not repeat the import of the same module, however, repeated import may affect some performance. Sometimes to achieve lazy loading (that is, to load another module with a high overhead), you can do this:
Email = Nonedef parse_email (): global email if email is None: import email... # this way, the email module will only be introduced once, when parse_email () is called for the first time.