After a combination of List. RemoveAll and List. FindAll produces performance problems...

Source: Internet
Author: User

Original code:

Data Volume in entityList: 4704; length: 2

Private static void OperateRemove (List <Entity> entityList, int length ){

EntityList. RemoveAll (delegate (Entity entity ){
List <Entity> list = entityList. FindAll (delegate (Entity innerEntity ){
Return entity. A = innerEntity.

});
Return null! = List & list. Count <length;
});

}

Solution 1: it is initially considered that the data is not sorted, so FindAll will be slow. So

Private static void OperateRemove (List <Entity> entityList, int length ){

EntityList. Sort (delegate (Entity entity1, parameter roompriceentity entity2 ){
Return entity1.A. CompareTo (entity2.A );
});

EntityList. RemoveAll (delegate (Entity entity ){
List <Entity> list = entityList. FindAll (delegate (Entity innerEntity ){
Return entity. A = innerEntity.

});
Return null! = List & list. Count <length;
});

}

At that time, after the program ran, the microsecond time was: 7901

 

Solution 2: It seems that the problem bottleneck is still in FindAll. Therefore, you can search for the entity. A to be deleted in one query and then delete it in A centralized manner.

Private static void OperateRemove (List <Entity> entityList, int length ){

EntityList. Sort (delegate (Entity entity1, Entity entity2 ){
Return entity1.A. CompareTo (entity2.A );
});
Int eachACount = 0;
Int A = 0;
Int nextA = 0;
List <int> lackAEntityList = new List <int> ();
Foreach (Entity entity in entityList ){
If (0 = ){
A = entity.;
NextA = entity.;
}
Else {
NextA = entity.;
}
If (! = NextA ){
If (length> eachACount ){
LackAEntityList. Add (roomId );
}
A = nextA;
EachACount = 0;
}
EachACount ++;
}
EntityList. RemoveAll (delegate (Entity entity ){
Return lackAEntityList. Exists (delegate (int ){
Return entity. A =;
});
});

}

After the program ran,Microseconds: 40

 

Of course there are other solutions: such as using sortlist + binarySearch, Etc.

However, 40 is ideal for us.

 

Attached

Objectives:
/// Search for the RoomID in B-List as 0 based on the role ID in A-List,
/// Delete the configuration information about all roomids with null values in B-List.
/// You must also delete the information in A-List.

Public List <RoomEntity> RemoveRoomIDAllEmpty ()
{
/// Create a consumer idlist for storing a non-empty RoomID
List <Int32> incluidlist = new List <int> ();

RoomList. ForEach (delegate (RoomEntity re)
{
If (hotelList. Contains (re. Upload ID ))
{
If (re. roomId! = 0)
{
Repeated idlist. Add (re. Upload ID );
}
}
});

// Delete the configuration information about all roomids of the role ID that are empty in the RoomList.
RoomList. RemoveAll (delegate (RoomEntity re)
{
Return! Repeated idlist. Contains (re. Upload ID );
});

// Delete the corresponding information in HotelList
HotelList. RemoveAll (delegate (Int32 I)
{
Return! HotelIdList. Contains (I );
});

Return roomList;
}

List <Int32> hotelList = new List <Int32> {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};

List <RoomEntity> roomList = new List <RoomEntity> {
New RoomEntity {role id = 1, roomId = 1111 },
New RoomEntity {role id = 1, roomId = 0 },
New RoomEntity {role id = 1, roomId = 1111 },
New RoomEntity {role id = 1, roomId = 0 },
New RoomEntity {role id = 1, roomId = 1111 },
New RoomEntity {role id = 2, roomId = 0 },
New RoomEntity {role id = 2, roomId = 0 },
New RoomEntity {role id = 2, roomId = 0 },
New RoomEntity {role id = 2, roomId = 0 },
New RoomEntity {role id = 4, roomId = 1111 },
New RoomEntity {role id = 5, roomId = 1111 },
New RoomEntity {role id = 3, roomId = 1111 },
New RoomEntity {role id = 4, roomId = 1111 },
New RoomEntity {role id = 1, roomId = 1111 },
New RoomEntity {role id = 6, roomId = 1111 },
New RoomEntity {role id = 1, roomId = 1111 },
New RoomEntity {role id = 5, roomId = 1111 },
New RoomEntity {role id = 8, roomId = 1111 },
New RoomEntity {role id = 7, roomId = 0 },
New RoomEntity {role id = 4, roomId = 1111 },
New RoomEntity {role id = 8, roomId = 1111 },
New RoomEntity {Region ID = 9, roomId = 1111 },
New RoomEntity {role id = 10, roomId = 0 },
New RoomEntity {role id = 10, roomId = 0}
};

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.