Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Python acceleration skills?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the Python acceleration skills". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what Python acceleration skills".

1. Avoid using global variables

Import mathsize = 10000 for x in range (size): for y in range (size): Z = math.sqrt (x) + math.sqrt (y)

Many programmers start by writing simple scripts in Python. When writing a script, you usually use global variables directly, like the above code.

However, due to the different implementation of global variables and local variables, the code defined in global variables runs much slower than the functions defined in functions. If you put script statements into a function, you can usually increase the running speed by 15% and 30%. As follows:

Import mathdef main (): size = 10000 for x in range (size): for y in range (size): Z = math.sqrt (x) + math.sqrt (y) main ()

two。 Avoid data duplication

Avoid meaningless data replication

Def main (): size = 10000 for _ in range (size): value = range (size) value_list = [x for x invalue] square_list = [x * x for x invalue_list] main ()

In this code, value_list is completely unnecessary, which creates unnecessary data structures or replication.

Def main (): size = 10000 for _ in range (size): value = range (size) square_list = [x * x for x invalue] main ()

Another reason is that Python's data sharing mechanism is too paranoid to understand or trust the memory model, such as abusing the copy.deepcopy () function. We can delete replication operations in this type of code.

There is no need to use intermediate variables when exchanging values

Def main (): size = 1000000 for _ in range (size): a = 3b = 5 temp = an a = b b = tempmain ()

The above code creates a temporary variable temp when exchanging values. Without intermediate variables, the code is cleaner and runs faster.

Def main (): size = 1000000 for _ in range (size): a = 3b = 5a, bb = b, amain ()

Use the character concatenation method join instead of'+'

Import string from typing import Listdef concatString (string_list: List [str])-> str: result =''for str_i in string_list: result + = str_i return resultdef main (): string_list = list (string.ascii_letters * 100) for _ in range (10000): result = concatString (string_list) main ()

Another important point is that abinb concatenates strings, and because strings are immutable objects in Python, an and b are actually copied into the application's new memory space, respectively.

Therefore, if concatenating n strings produces "nmur1" intermediate results, each string produces the intermediate results required to apply and copy memory, which seriously affects the efficiency of the operation.

When concatenating strings with join (), you first calculate the total memory space that needs to be applied, then immediately request the required memory, and then copy each string element into memory.

Import string from typing import Listdef concatString (string_list: List [str])-> str: return''.join (string_list) defmain (): string_list = list (string.ascii_letters* 100) for _ in range (10000): result = concatString (string_list) main ()

3. Avoid using the following function properties

Avoid accessing module and function properties

Import mathdef computeSqrt (size:int): result = [] for i in range (size): result.append (math.sqrt (I) return resultdef main (): size = 10000 for _ in range (size): result = computeSqrt (size) main ()

The use (attribute access operator) triggers specific methods, such as getattribute () and getattr (), which perform dictionary operations and incur additional time consumption.

By using the import statement, you can eliminate attribute access:

From math import sqrtdefcomputeSqrt (size: int): result = [] for i in range (size): result.append (sqrt (I)) return resultdef main (): size = 10000 for _ in range (size): result = computeSqrt (size) main ()

We discussed earlier that local variables can be found faster than global variables, and frequently accessed variables (such as sqrt) can be changed to local variables to speed up the operation.

Import mathdef computeSqrt (size:int): result = [] sqrt = math.sqrt for i in range (size): result.append (sqrt (I)) return resultdef main (): size = 10000 for _ in range (size): result = computeSqrt (size) main ()

Avoid class attribute access

Import math from typing import Listclass DemoClass: def _ _ init__ (self, value: int): self._value = value def computeSqrt (self Size: int)-> List [float]: result = [append = result.append sqrt = math.sqrt for _ in range (size): append (sqrt (self._value)) return resultdef main (): size = 10000 for _ in range (size): demo_instance = DemoClass (size) result = demo_instance.computeSqrt (size) main ()

The principle of avoidance also applies to the properties of a class, and accessing self._value is slower than accessing local variables. Code execution speed can be improved by assigning class attributes that need to be accessed frequently to local variables.

Import math from typing import Listclass DemoClass: def _ _ init__ (self, value: int): self._value = value def computeSqrt (self Size: int)-> List [float]: result = [] append = result.append sqrt = self._value for _ in range (size): append (sqrt (value)) return resultdef main (): size = 10000 for _ in range (size): demo_instance = DemoClass (size) demo_instance.computeSqrt (size) main ()

4. Avoid unnecessary abstractions

Class DemoClass: def _ init__ (self, value: int): self.value = value@property def value (self)-> int: return self._value@value.setter def value (self, x: int): self._value = xdef main (): size = 1000000 for i in range (size): demo_instance = DemoClass (size) value = demo_instance.value demo_instance.value = imain ()

Whenever code is encapsulated with other processing layers (such as decorators, property access, descriptors), the code runs more slowly. In most cases, it is necessary to re-check whether it is necessary to use a property accessor definition.

Using the getter/setter function to access properties is usually a coding style forgotten by C _ plumber + programmers. If it's really not necessary, just use simple attributes.

Class DemoClass: def _ init__ (self, value: int): self.value = valuedef main (): size = 1000000 for i in range (size): demo_instance = DemoClass (size) value = demo_instance.value demo_instance.value = imain ()

5. Select the appropriate data structure

As we all know, lists are dynamic arrays in Python. When the pre-allocated memory space is used up, a certain amount of memory space is pre-allocated and then elements continue to be added to it. Then copy all the original elements to form a new memory space, and destroy the previous memory space before inserting the new element.

Therefore, if you add or delete frequently, or if the number of elements added or removed is too large, the list will become less efficient, and it is best to use collections.deque for now.

This double-ended queue has the characteristics of stack and queue, and insert and delete operations can be performed at both ends with O (1) complexity.

List search operations are very time-consuming. When you need to find certain elements frequently or access them frequently in order, use dichotomy while keeping the list objects in order, and use binary search to improve search efficiency, but binary search is only suitable for ordered elements.

Another common requirement is to find the minimum or maximum value. At this point, you can use the heapq module to list the list converted to the heap, so the time complexity of getting the minimum value is O (1).

6. Cycle optimization

Use for loops instead of while loops

Def computeSum (size: int)-> int: sum_ = 0 I = 0 while I

< size: sum_ += i i += 1 return sum_def main(): size = 10000 for _ in range(size): sum_ = computeSum(size)main() 要知道,Python中的for循环要比while循环快得多。 def computeSum(size: int) ->

Int: sum_ = 0 for i in range (size): sum_ + = I return sum_def main (): size = 10000 for _ in range (size): sum_ = computeSum (size) main ()

Use implicit for loops instead of explicit for loops

For the above example, you can further replace the explicit for loop with an implicit for loop

Def computeSum (size: int)-> int: return sum (range (size)) def main (): size = 10000 for _ in range (size): sum = computeSum (size) main

Reduce the calculation of internal loops

From math import sqrtdef main (): size = 10000 for x in range (size): for y in range (size): Z = sqrt (x) + sqrt (y) main ()

In the code sqrt (x) in the for loop above, recalculations are required every time during training, which increases time consumption.

Import mathdef main (): size = 10000for x in range (size): sqrtsqrt_x = sqrt (x) for y in range (size): Z = sqrt_x + sqrt (y) main ()

7. Use numba.jit

Continue to follow the above example and use numba.jit on that basis. The Python function JIT can be compiled into machine code for execution, which can greatly improve the speed of code execution.

Import numba@numba.jit def computeSum (size: float)-> int: sum = 0 for i in range (size): sum + = i return sumdef main (): size = 10000 for _ in range (size): sum = computeSum (size) main ()

8. Code optimization principle

The first basic principle is not to optimize your code prematurely.

Many people focus on performance optimization when they start writing code. "it's much easier to speed up the right program than to ensure that the fast program works correctly." The premise of optimizing the code is to ensure that the code works properly. Premature optimization may ignore the mastery of the overall performance metrics and do not reverse the order until the overall results are obtained.

The second basic principle is to weigh the cost of optimizing the code.

Optimizing the code comes at a cost, and it's almost impossible to solve all the performance problems. Usually faced with the choice is time for space or space for time, but also need to consider the development cost.

The third principle is not to optimize the unimportant parts.

If you optimize each part of the code, these changes will make the code difficult to read and understand. If the code is slow, you must first find where the code is slow (usually an internal loop), and focus on optimizing where the code is slow. For other locations, the loss of time has little impact.

Thank you for your reading, the above is the content of "what are the Python acceleration skills". After the study of this article, I believe you have a deeper understanding of what Python acceleration skills you have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report