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

How to use iterator design pattern to improve Python performance

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use iterator design patterns to improve the performance of Python". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Simple case

Before we begin to introduce design patterns, let's look at a simple requirement. Suppose now we need to get the first few days of the week according to the variables passed in. For example, we return [Mon, Tue, Wed] when we pass in 3, and [Mon, Tue, Wed, Thu, Fri] by passing in 5. This requirement should be understood by everyone, and it is very simple.

If you implement it with a function, it's like this:

Def return_days (n): week = ['Mon',' Tue', 'Wed',' Thu', 'Fri',' Sat', 'Sun'] return week [: n]

If you look at three lines of code, there is no problem with writing this way in this problem scenario. But if we change the title a little bit, the week here is not a fixed data, but read from the upstream or a file. The n here is also a very large number, and we rewrite this function like this:

Def get_data (n): data = [] for i in range (n): data.append (get_from_upstream ()) return data

Let's assume that the get_from_upstream function implements the specific logic of getting data, so what's the problem with the above function?

Some students will say that this is not a problem, because it is also done in other languages when implementing data acquisition. Indeed, languages such as Java may do the same. But just because other languages are right to do this doesn't mean that Python is right to do it. Because we didn't make the most of Python's ability.

There are two problems here, the first one is delay, because as I said earlier, n is a very large number. Getting data from upstream, whether through the network or file, is essentially an IO operation, and the latency of IO operation is very large. Then it may take a long time for us to collect all these n pieces of data, resulting in a long wait downstream. The second problem is memory, because we store these n pieces of data to return together, if n is very large, the overhead pressure on memory is also very large, if the machine does not have enough memory, it is likely to cause a crash.

Then how to solve it?

In fact, the solution is very simple, if you are familiar with iterators, you will find that iterators are aimed at these two problems. We can rewrite the above logic into an iterator implementation, which is the iterator pattern.

Iterator mode

Strictly speaking, the iterator pattern is actually an application of iterators. It skillfully combines iterators with anonymous functions, and there are not many ways to say. Let's rewrite the code just now, and the details are all in the code.

Def get_data (n): for i in range (n): yield get_from_upstream () data_10 = lambda: get_data (10) data_100 = lambda: get_data # use for d in data_10: print (d)

It's very simple, but you may want to ask, since we have written get_data this iterator, we should just use for d in get_data (10) when we use it. Why should we use anonymous functions to wrap a layer in the middle?

The reason is also very simple, if this data is used by ourselves, of course, there is no need for a layer of tundish. But if we pass it to the downstream, for the downstream, it certainly does not want to consider too many details upstream, the simpler the better. So let's just throw in a packaged iterator and call directly downstream. Otherwise, the downstream also needs to be aware of the parameters passed in by the get_data function, which is obviously not reasonable.

That's all for "how to improve Python performance with iterator design patterns". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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