In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "what are the skills to speed up Python runtime". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
0. optimization principle
Before we dive into the details of code optimization, we need to understand some of the fundamentals of code optimization.
Make sure the code works properly first. Because it's much easier to make the right program quickly than it is to make a fast program.
Weighing optimization costs. Optimization comes at a price. For example, fewer runtimes generally require more space usage, or fewer runtimes generally require more runtime.
Optimization cannot sacrifice code readability.
1. Correct data type usage in Python
1.1 Replace list with set to check if elements are in the sequence
Depending on Python's time complexity, the average case of x in s operations of list is O(n). On the other hand, the average case of x in s operations of a set is O(1).
1.2 Initialize the dictionary using defaultdict
We should initialize with defaultdict.
2. Bad: 447ms nums_sum_list_interpretation = sum([num**2 for num in range(100000)]) # Good: 300 msnums_sum_generator_expression = sum((num**2 for num in range(100000))
Another benefit of generator expressions is that we don't need to iterate to get results without building and saving the entire list object in memory. In other words, generator expressions save memory usage.
import sys # Badnums_squared_list = [num**2 for num in range(100000)] print(sys.getsizeof(nums_squared_list)) # 87632 # Goodnums_squared_generator = (num**2 for num in range(100000)) print(sys.getsizeof(nums_squared_generator)) # 1283. Replace global variables with local variables
We should put global variables into functions. Local variables are faster than global variables.
4. Avoid dot operations
4.1 Avoid function access
Every time we use it. To access this function, it triggers specific methods such as__getattribute __() and__getattr __(). These methods will use dictionary operations, which will incur time costs. We can eliminate such charges by importing xx from xx.
According to technique 3, we can also assign global functions to local functions.
In addition, we can assign the list.append() method to native functions.
4.2 Avoid class attribute access
Visit self._ Value is slower than accessing local variables. We can assign class attributes to local variables to speed up runtime.
5. Avoid unnecessary abstractions
This slows down code when wrapped with other processing layers (e.g. decorators, attribute access, descriptors). In most cases, it is necessary to reconsider whether these layers are necessary. Some C / C++ programmers may follow a coding style that uses getter / setter functions to access properties. But we can use simpler writing styles.
6. Avoid duplication of data
6.1 Avoid meaningless data duplication
value_list is meaningless.
6.2 Avoid temp variables when changing values
Temporary variables are not required.
6.3 Replace + with join() when connecting strings
When you concatenate strings using a + b, Python will request memory space and copy a and b separately into the memory space of the new app. This is because string data types in Python are immutable objects. If n strings are concatenated, it generates n-1 intermediate results, and each intermediate result is applied to memory space and copies the new string.
On the other hand, join() will save time. It will first calculate the total memory space that needs to be applied, then apply the required memory at once, and then copy each string element into memory.
7. Short-circuit evaluation using if statements
Python uses short-circuiting techniques to speed up evaluation of truth values. If the first statement is false, the whole thing must be false, so it returns that value. Otherwise, if the first value is true, check the second value and return it.
Therefore, to save runtime, we can follow the following rules:
if a and b: Variable a should have a high probability of False, so Python doesn't compute b.
if a or b: Variable a should have a high probability of True, so Python doesn't compute b.
8. loop optimization
8.1 for
For loops are faster than while loops.
8.2 Replace explicit for loops with implicit for loops
8.3 Reduce computation of internal for loops
We move sqrt(x) from the inner for loop to the outer for loop.
9. Use numba.jit
Numba can JIT compile Python functions into machine code for execution, greatly increasing code speed. For more information about numba, see the home page.
We use the example above.
We move sqrt(x) from the inner for loop to the outer for loop.
10. Find the Time Cost Function Using cProfile
cProfile will output the time usage of each function. So we can find the time cost function.
"What are the tricks to speed up Python runtime" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.