In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the suggestions for Python performance optimization". In daily operation, I believe that many people have doubts about the suggestions for Python performance optimization. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the suggestions for Python performance optimization?" Next, please follow the editor to study!
1. Optimize the time complexity of the algorithm
The time complexity of the algorithm has the greatest impact on the execution efficiency of the program. In Python, the time complexity can be optimized by selecting the appropriate data structure, for example, the time complexity of list and set to find an element is O (n) and O (1), respectively. Different scenarios have different optimization methods, generally speaking, there are general ideas such as divide and conquer, branch and boundary, greed, dynamic planning and so on.
2. Reduce redundant data
Such as using the upper triangle or the lower triangle to preserve a large symmetric matrix. Sparse matrices are used in matrices with 0 elements in the majority.
3. Rational use of copy and deepcopy
For objects with data structures such as dictlist, direct assignment is done by reference. In some cases where you need to copy the entire object, you can use copy and deepcopy in the copy package. The difference between these two functions is that the latter is copied recursively. The efficiency is also different: (the following programs run in ipython)
Import copy
A = range (100000)
% timeit-n 10 copy.copy (a) # run copy.copy (a) 10 times
% timeit-n 10 copy.deepcopy (a)
10 loops, best of 3: 1.55 ms per loop
10 loops, best of 3: 151 ms per loop
The-n after timeit indicates the number of runs, and the last two lines correspond to the output of two timeit, the same below. It can be seen that the latter is an order of magnitude slower.
4. Use dict or set to find elements
Both python dict and set are implemented using the hash table (similar to unordered_map in the standard library of cymbals 11), and the time complexity of finding elements is O (1).
A = range (1000)
S = set (a)
D = dict ((iPol 1) for i in a)
% timeit-n 10000 100in d
% timeit-n 10000 100in s
10000 loops, best of 3: 43.5 ns per loop
10000 loops, best of 3: 49.6 ns per loop
Dict is slightly more efficient (and takes up a little more space).
5. Rational use of generators (generator) and yield
% timeit-n 100a = (i for i in range (100000))
% timeit-n 100b = [i for i in range (100000)]
100 loops, best of 3: 1.54 ms per loop
100 loops, best of 3: 4.56 ms per loop
What you get with () is a generator object, and the memory required is independent of the size of the list, so it's more efficient. In specific applications, for example, set (i for i in range (100000)) is faster than set ([i for i in range (100000)]).
But for cases that require loop traversal:
% timeit-n 10 for x in (i for i in range (100000)): pass
% timeit-n 10 for x in [i for i in range (100000)]: pass
10 loops, best of 3: 6.51 ms per loop
10 loops, best of 3: 5.54 ms per loop
The latter is more efficient, but if there is break in the loop, the benefits of using generator are obvious. Yield is also used to create generator:
Def yield_func (ls):
For i in ls:
Yield iTunes 1
Def not_yield_func (ls):
Return [item1 for i in ls]
Ls = range (1000000)
% timeit-n 10 for i in yield_func (ls): pass
% timeit-n 10 for i in not_yield_func (ls): pass
10 loops, best of 3: 63.8 ms per loop
10 loops, best of 3: 62.9 ms per loop
For list whose memory is not very large, you can return a list directly, but the readable yield is better (personal preference).
Python2.x built-in generator functions include xrange functions, itertools packages, and so on.
6. Optimize the cycle
Don't put in the loop what you can do outside the loop, for example, the following optimizations can be twice as fast:
A = range (10000)
Size_a = len (a)
% timeit-n 1000 for i in a: K = len (a)
% timeit-n 1000 for i in a: K = size_a
1000 loops, best of 3: 569 μ s per loop
1000 loops, best of 3: 256us per loop
7. Optimize the order of multiple judgment expressions
For and, we should put those that meet fewer conditions first, and for or, put those that meet more conditions first. Such as:
A = range (2000)
% timeit-n 100 [i for i in an if 10
< i < 20 or 1000 < i < 2000] %timeit -n 100 [i for i in a if 1000 < i < 2000 or 100 < i < 20] %timeit -n 100 [i for i in a if i % 2 == 0 and i >1900]
% timeit-n 100 [i for i in an if i > 1900 and I% 2 = = 0]
100 loops, best of 3: 287 μ s per loop
100 loops, best of 3: 214 μ s per loop
100loops, best of 3: 128us per loop
100 loops, best of 3: 56.1 μ s per loop
8. Use join to merge strings in the iterator
In [1]:% timeit
S =''.
...: for i in a:
S + = I
...:
10000 loops, best of 3: 59.8 μ s per loop
In [2]:% timeit
S ='. Join (a)
...:
100000 loops, best of 3: 11.8 μ s per loop
Join has an improvement of about 5 times for the cumulative approach.
9. Choose the appropriate way to format characters
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,% is the slowest, but the gap between the three is not large (all very fast). (personally, I think% is the most readable.)
10. Exchange the values of two variables without the help of intermediate variables
In [3]:% timeit-n 10000
A recorder baccalaureate 1 pas 2
...: cedar _ a _ b _ b
....:
10000 loops, best of 3: 172 ns per loop
In [4]:% timeit-n 10000
A recorder baccalaureate 1 pas 2
A _
....:
10000 loops, best of 3: 86 ns per loop
The value of a memory b can be more than twice as fast in exchange for the value of a quotation b in exchange for the value of a quotation b, instead of cantilever b.
11. Use if is
A = range (10000)
% timeit-n 100 [i for i in an if i = = True]
% timeit-n 100 [i for i in an if i is True]
100 loops, best of 3: 531 μ s per loop
100 loops, best of 3: 362 μ s per loop
Using if is True is nearly twice as fast as if = = True.
Use cascading to compare x < y < z
X, y, z = 1, 2, 2, 3
% timeit-n 1000000 if x < y < z:pass
% timeit-n 1000000 if x < y and y < z:pass
1000000 loops, best of 3: 101 ns per loop
1000000 loops, best of 3: 121 ns per loop
X < y < z is slightly more efficient and more readable.
13. While 1 is faster than while True
Def while_1 ():
N = 100000
While 1:
N-= 1
If n
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.