Python Performance optimizations

Source: Internet
Author: User
Tags pow shallow copy

1. Optimized circulation

What you can do outside of the loop, do not put in the loop, such as the following optimization can be one times faster

2. Use join to merge strings in iterators

joinFor the cumulative way, there is an increase of about 5 times times

3. Useif is

Use if is True if == True nearly one-fold faster than

4. Using cascading comparisons x < y < z

x < y < zSlightly more efficient and more readable

5. Use **Rather than the POW.
%timeit-n 10000 c = POW (2,20)%timeit-n 10000 c = 2**2010000 loops, best of 3:284 ns per loop10000 loops, best of 3:16. 9 ns per loop

  **is faster than 10 times times!

6. Optimize the order of the expressions with multiple judgments

For and, you should put the conditions of satisfaction in front of, for or, put the conditions to meet more in front

A = Range (%timeit-n)  [I for I in a If < I < or + < I < 2000]%timeit-n [I for i] A If < i < 0 < i <     [%timeit-n] [I for I in a if I% 2 = = 1900]%timeit n [I for I in a If i > 1900 and i% 2 = = 0]100 loops, Best of 3:287μs per loop100 loops, best of 3:214μs per lo op100 loops, Best of 3:128μs per loop100 loops, Best of 3:56.1μs per loop

7. Use Dict or set to find elements

Python dict and set are all implemented using hash tables, and the time complexity of finding elements is O (1)

8. Reasonable use of copy and Deepcopy

For objects such as data structures such as dict and lists, the direct assignment uses a reference method. In some cases, you need to copy the entire object, and you can use copy and deepcopy in the copy package.

The difference between the two functions is that the latter is recursive replication. The efficiency is not the same: (The following program runs in Ipython)

Import Copya = range (100000)%timeit-n copy.copy (a) # run 10 times copy.copy (a)%timeit-n copy.deepcopy (a) loops, best of 3:1.55 ms per LOOP10 loops, best of 3:151 ms Per loop


9. Using list parsing and builder expressions

Table parsing is more efficient than rebuilding a new list in a loop, so we can use this feature to improve the efficiency of the operation.

1  fromTimeImport Time2t =Time ()3List = ['a','b',' is','python','Jason','Hello','Hill',' with','Phone','Test',  4  'DFDF','Apple','PDDF','IND','Basic','None','BAECR','var','Bana','DD','WRD']  5Total=[]  6   forIinchRange (1000000):  7   forWinchlist:8 Total.append (W)9  Print "Total run time:" Ten  PrintTime ()-T

Use list parsing:

    1. For I in Range (1000000):
    2. A = [w for W in list]

The above code runs approximately 17s, and the run time is reduced to 9.29s after using list resolution instead. Nearly half of the increase. The generator expression is the new content introduced in 2.4, syntax and list parsing is similar, but in large data processing, the advantage of the generator expression is more obvious, it does not create a list, just return a generator, so the efficiency is high. In the above example, the Code a = [w for w in list] is modified to a = (W for w in list), the running time is further reduced, shortening about 2.98s.

10. Use Xrange instead of range
Using xrange instead of range while looping, using Xrange can save a lot of system memory because Xrange () produces only one integer element per call in the sequence. The range () will return the complete list of elements directly, with unnecessary overhead for looping. Xrange no longer exists in the Python3, and the inside range provides a iterator that can traverse a range of any length.

11. Using Local Variables

Use local variables to avoid the "global" keyword. Python accesses local variables much faster than global variables, so you can use this feature to improve performance

12. Use is not
If do is isn't none faster than statement if done! = None

13, while 1 is faster than while Trule

Many Python-optimized articles will talk about this. So, how much can it improve? Let's try it.

1 ImportRandom2 Import Time3 4Start_time =time.time ()5 Printstart_time6   7j = 18  whileTrue:9J + = 1TenEnd_time =time.time () One     ifEnd_time-start_time >= 1 :   A          Break   - PrintJ - PrintEnd_time the  - Print "======== Split ==========" -Start_time =time.time () - Printstart_time +    -j = 1 +  while1:   AJ + = 1 atEnd_time =time.time () -     ifEnd_time-start_time >= 1 :   -          Break   - PrintJ - PrintEnd_time

Output Result:

1399342863.16
2573550
1399342864.16
======== Split ==========
1399342864.18
2973070
1399342865.18

One is 250,000, one is 290,000. About 16% more performance. Actually, it's not obvious. Just better than nothing.

14, using built-in functions faster, such as add (A, b) faster than a+b

15, if the need to exchange two variables of the value of using A,b=b,a instead of the use of intermediate variable t=a;a=b;b=t;

16. Generating sequences

Use the range () function to generate a sequence with a custom sequence

(1) A = range (0,6)

(2) A = [0, 1, 2, 3, 4, 5]

Tested separately, the results are as follows:

loop_num:1029877

loop_num:1602341

Conclusion: It is also more efficient to define sequences by themselves explicitly.

17. Generating a copy of a sequence

Generate a copy of a sequence: with Copy, with slice properties

A = [0, 1, 2, 3, 4, 5]

(1) b = copy.copy (a)

(2) b = a[:]

Tested separately, the results are as follows:

loop_num:677838

loop_num:1530012

Conclusion: The modified slice application is more efficient than the shallow copy function as a copy. Note that deep copy efficiency is low!

18. Use Python built-in functions with caution

Python built-in functions just to deal with common situations. In many cases, the performance of built-in functions is far inferior to those written by themselves and targeted functions. Moving hands, change the algorithm, you can improve performance more than one times.

19. Choose the appropriate format character method
S1, s2 =' Ax ', ' bx '%timeit-n 100000  ' abc%s%s '% (S1 , s2)%timeit-n 100000  ' abc{0}{1} '. Format (s1, S2)%timeit-n 100000  ' abc ' + s1 + s2 100000 loops, best of  3: 183 ns per loop 100000 loops, best of  3: 169 ns per loop 100000 loops, best of  3: 103 ns per loop         

Of the three cases, the% is the slowest, but the difference between the three is not large (both very fast). (Individuals feel the best readability of%)

20. Reasonable use of Del

With Del you can set the reference count value of the object to occupy memory space by 0 (deletion of a name removes the binding of that name from the local or global namespace). It does not allow the memory occupied by the object to be recycled, but the reference count of a memory becomes 0, which means it can be reused again (so del, unnecessary GC intervention).

If del is not used, the following code may Memoryerror

1 Import NumPy as NP 2 3 matrix1 = Np.zeros ((60000,100000))4 matrix2 = Np.zeros ((60000,100000))5  #  using matrix16#  using matrix2

Del, you can use the unused memory after the object is deleted, the following code on the memory cost is not large.

1 Import NumPy as NP 2 3 matrix1 = Np.zeros ((60000,100000))4#  using matrix15del  matrix167 matrix2 = Np.zeros ((60000,100000))8#  using matrix29del matrix2

Python Performance optimizations

Related Article

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.