Optimization of Nested Loops

Source: Internet
Author: User

Requirements background

Today we have a requirement: Map A and map B, all two maps hold a similar key--id, and their corresponding value may or may not be the same. It is now necessary to do something special with the same key value pair as the key corresponding to the value in the two map.

This is a very simple need, the code is very simple, I directly in a loop nested another loop to achieve this function requirements:

for(Map.Entry<String, String> entry : mapA.entrySet()) {    //do something,需要循环10次    for(Map.Entry<String, String> entry : mapB.entrySet()){          //do something,需要循环1000次    }}

Write the time also did not consider too much, submit the code to the group leader review, the team leader said the loop nested here is not good, because in the actual business, set B will be relatively large, assuming that the size of Mapa is 10,MAPB size is 1000, so write need to cycle 10*1000 times, After all, the loop needs a series of operations, if a lot of people at the same time through the UI to trigger this logic, there may be a performance problem, for users, if you click on a button on the UI to wait 10 seconds to have the result, it is a devastating user experience.

So when you encounter this need to nest loops, you should minimize the number of cycles, in addition, in general, cycle is placed inside the small loop outside, also improve performance.

An optimization idea

According to the team leader's suggestion, I can reduce the cycle times of the internal cycle as much as possible, which is the total number of cycles of n*m, which can be divided into n+m as much as possible according to business requirements. Of course, it is not possible to really split into N+m, just try to move in this direction.

To achieve this optimization, you can only group internal large loops. How do you group it exactly? You can add a new map and then group by ID (this is because the IDs in my business needs are duplicated, so ID is grouped by). The data of the same ID is divided into a group and stored in a ArrayList, and the ID is stored as a key in the map, and the ArrayList is stored as value in the map.

Assuming that the original internal large loop set size is 1000, we divide it into 10 groups, and the outer small loop of the set size is 10, then the original1000 total cycle times can be changed to form 1000+1010 times. As follows:

for(Map.Entry<String, String> entry : mapB.entrySet()){     //先对大集合Map B进行分组,并存入一个Map C中,需要循环1000次}for(Map.Entry<String, String> entry : mapA.entrySet()) {    //do something,需要循环10次    for(Map.Entry<String, String> entry : mapC.entrySet()){          //do something,需要循环10次    }}

Of course, this optimization idea is in specific functional requirements can be achieved, specific problems specific analysis, because the leader of the reminder, I know that the original nested loops can also be optimized, the code of the road is to be accumulated.

In addition to the specific analysis of the cycle in the small cycle outside, you can look at this article: For loop nesting efficiency

But for the time being I still can't read.

Optimization of Nested Loops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.