Python Code Optimization Tips

Source: Internet
Author: User

Code optimization Part1

Share some of the tips you've seen recently about code optimization.

Short-circuit characteristics if judged

For and, the satisfying condition should be placed in the front, so that when a large number of judgments, the condition of a few satisfied conditions directly back to the other expression will not be computed thereby saving time (because false and True or false)

Import timeits1 = "" "A = range (+) [I for I in a if I% 2 ==0 and i > 1900]" "" S2 = "" "A = range (+) [I for I in a if< C0/>i > 1900 and I% 2 ==0] "" "Print Timeit.timeit (stmt=s1, number=1000) print Timeit.timeit (STMT=S2, number=1000)

The results of the operation are as follows:

➜  python test6.py0.2485320568080.195827960968# can see that S2 expression calculations are faster because most cases do not meet i>1900, so in these cases I% 2 = = 0 is not calculated, thus saving time Room

Similarly, for or, put the conditions of satisfaction in front.

Import timeits1 = "" A = range (+) [I for I in a If < I <20 or + < I <] "" "S2 =" "" A = Range (2000) [ I for I in a If < I < + or < I <20] "" "Print Timeit.timeit (stmt=s1, number=1000) print Timeit.timeit ( STMT=S2, number=1000)

Operation Result:

0.2531249523160.202992200851

Join Merge String

Join merge strings are faster than loops using + to merge.

Import timeits1 = "" A = "" "A = [str (x) for x in range"]s = "For I in a:    s + = i" "" S2 = "" "A = [str (x) for x in range (20 ]s = '. Join (a) "" "Print Timeit.timeit (stmt=s1, number=1000) print Timeit.timeit (STMT=S2, number=1000)

The results of the operation are as follows:

Python test6.py0.5589458942410.422435998917

While 1 and while True

In python2.x, True and False are not reserved keywords, it is a global variable, which means that 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 results of the operation are as follows:

➜  python test6.py5.180073022846.84624099731

Because each time you judge while true, you first need to find the value of true.

In python3.x, True becomes the keyword argument, so the two cases are the same.

CProfile, Cstringio and Cpickle

Versions written in the C language are faster than native. Cpickle vs Pickle are 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 results of the operation are as follows:

➜python test6.py0.1821789741521.70917797089

Rational use of generators

Difference

using () is a generator object that requires no more memory space than the size of the list, so the efficiency is higher.

Import timeits1 = "" "[I for I in range (100000)]" "" S2 = "" "" "" "" "" "Print Timeit.timeit (STMT=S1, Numbe r=1000) Print Timeit.timeit (STMT=S2, number=1000)

Results:

➜  python test6.py5.443274974820.923446893692

But for situations where loop traversal is required: using iterators is inefficient, as follows:

Import timeits1 = "" ls = Range (1000000) def yield_func (LS): for    i in LS:        yield i+1for x in Yield_func (LS):    pas S "" "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 results are as follows:

➜  python test6.py1.031867027281.01472687721

So the use of generators is a tradeoff for the results of memory, speed synthesis considerations.

Xrange

In python2.x, Xrange is a pure C-implemented generator, and it does not compute all the values in memory at once, relative to range. But its limit is only to work with the integral type: You cannot use long or float.

The cost of the import statement

Import statements sometimes to limit their scope or to save initialization time, the inside of the unloaded function, although the Python interpreter does not duplicate the import of the same module without error, but repeated import will affect some of the performance. Sometimes in order to implement lazy loading (that is, to load a very expensive module when used), you can do this:

email = nonedef Parse_email ():    Global Email    If email is None:        import Email ...    # so the email module will only be introduced once, At the time of Parse_email () being called for the first time.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.