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 performance optimization skills of python?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Xiaobian to share with you what python performance optimization skills, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Performance Optimization Tips

1. Limit CPU and memory usage

If Python programs consume too many resources and want to limit their use, you can use resource packages.

# CPU limit def time_exceeded(signo, frame): print("CPU exceeded... ") raise SystemExit(1) def set_max_runtime(seconds): soft, hard = resource.getrlimit(resource.RLIMIT_CPU) resource.setrlimit(resource. RLIMIT_CPU, (seconds, hard)) signal.signal(signal.SIGXCPU, time_exceeded) #limit memory usage def set_max_memory(size): soft, hard = resource.getrlimit (resource. RLIMIT_AS, (size, hard))

When limiting CPUs, get the soft limit and hard limit for a particular resource (RLIMIT_CPU), and then set it using the number of seconds specified by the parameter and the hard limit obtained. If CPU time is exceeded, a signal is registered that causes the system to exit.

For memory limits, also get soft and hard limits and set them with setrlimit with the size parameter.

2. Save memory by__slots__

If you have a class in your program that needs to create a large number of instances, it can be very memory intensive. Because Python uses dictionaries to represent attributes of class instances, this can speed up execution, but memory efficiency is poor, which is usually not a problem. You can use__slots__to optimize:

import sys class FileSystem(object):def __init__(self, files, folders, devices): self.files = files self.folders = folders self.devices = devices print(sys.getsizeof( FileSystem )) class FileSystem1(object): __slots__ = ['files', 'folders', 'devices'] def __init__(self, files, folders, devices): self.files = files self.folders = folders self.devices = devices print(sys.getsizeof( FileSystem1 ))

# Python 3.5

#1-> 1016

#2-> 888

When defining__slots__attributes, Python uses fixed-size arrays as attributes rather than dictionaries, which greatly reduces the memory required per instance. Of course, there are disadvantages to using__slots__, such as not declaring any new attributes and using them only on__slots__, and classes of__slots__cannot use multiple inheritance.

3. Call with lru_cache cache function

Python is said to have poor performance, especially when it comes to some calculations. In fact, there are some common methods to solve the problem of program performance, such as caching and mnemonics. Using lru_cache in functools can solve the problem of a large number of repeated iterative calls in iterative calculations:

# CacheInfo(hits=2, misses=4, maxsize=32, currsize=4)

In the example above, we execute a GET request that is being cached (maximum of 3 cached results). The cache_info method is also used to check cache information for functions. Decorators also provide a clear_cache method for deleting caches.

4. __all__control import

Some languages support mechanisms for importing members (variables, methods, interfaces). In Python, everything is imported by default, but you can restrict it with__all__

def foo(): pass def bar(): pass __all__ = ["bar"]

In this way we can limit what can be imported when using from some_module import *. In this case, only the import bar function is used. If__all__is left blank and nothing is imported when wildcard import is used, an AttributeError error is triggered.

The above is "python performance optimization tips what" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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