• List.RemoveAll和List.FindAll结合产生的性能问题之后...


    原代码:

    当时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.A

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

    }

    第一方案:初步认为是数据未排序,所以FindAll会慢。所以

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

    entityList.Sort( delegate( Entity entity1, HotelRoomPriceEntity entity2) {
             return entity1.A.CompareTo( entity2.A);
    } );

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

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

    }

    当时程序跑过之后,微秒时间为:7901

     

    第二方案:看来问题瓶颈还是在FindAll,所以通过一次查询先把要删除的entity.A查找出来,后面集中删除。

    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) {
                        A= entity.A;
                        nextA= entity.A;
                    }
                    else {
                        nextA= entity.A;
                    }
                    if ( A != nextA) {
                        if ( length> eachACount ) {
                            lackAEntityList.Add( roomId );
                        }
                        A = nextA;
                        eachACount = 0;
                    }
                    eachACount ++;
                }
                entityList.RemoveAll( delegate( Entity entity) {
                    return lackAEntityList.Exists( delegate( int A) {
                        return entity.A== A;
                    } );
                } );

                }

    当时程序跑过之后,微秒时间为:40

     

    当然还有其他解决方案:如使用sortlist+binarySearch,Etc.

    不过40对我们来说,已经比较理想。

     

    另外附上

    目标:
            /// 根据A-List中的HotelID查找B-List中RoomID为0,
            /// 删除对应B-List中的有关于HotelID所有RoomId全为空的配备信息
            /// 同时也要删除A-List中对应的信息

            public List<RoomEntity> RemoveRoomIDAllEmpty()
            {
                ///创建存放RoomID不为空的HotelIDList
                List<Int32> hotelIdList = new List<int>();

                roomList.ForEach(delegate(RoomEntity re)
                {
                    if (hotelList.Contains(re.HotelId))
                    {
                        if (re.roomId != 0)
                        {
                            hotelIdList.Add(re.HotelId);
                        }
                    }
                });

                //删除RoomList中的有关于HotelID所有RoomId全为空的配备信息
                roomList.RemoveAll(delegate(RoomEntity re)
                {
                    return !hotelIdList.Contains(re.HotelId);
                });

                //删除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{ HotelId =1,roomId=1111},
                new RoomEntity{ HotelId =1,roomId=0},
                new RoomEntity{ HotelId =1,roomId=1111},
                new RoomEntity{ HotelId =1,roomId=0},
                new RoomEntity{ HotelId =1,roomId=1111},
                new RoomEntity{ HotelId =2,roomId=0},
                new RoomEntity{ HotelId =2,roomId=0},
                new RoomEntity{ HotelId =2,roomId=0},
                new RoomEntity{ HotelId =2,roomId=0},
                new RoomEntity{ HotelId =4,roomId=1111},
                new RoomEntity{ HotelId =5,roomId=1111},
                new RoomEntity{ HotelId =3,roomId=1111},
                new RoomEntity{ HotelId =4,roomId=1111},
                new RoomEntity{ HotelId =1,roomId=1111},
                new RoomEntity{ HotelId =6,roomId=1111},
                new RoomEntity{ HotelId =1,roomId=1111},
                new RoomEntity{ HotelId =5,roomId=1111},
                new RoomEntity{ HotelId =8,roomId=1111},
                new RoomEntity{ HotelId =7,roomId=0},
                new RoomEntity{ HotelId =4,roomId=1111},
                new RoomEntity{ HotelId =8,roomId=1111},
                new RoomEntity{ HotelId =9,roomId=1111},
                new RoomEntity{ HotelId =10,roomId=0},
                new RoomEntity{ HotelId =10,roomId=0}
            };

  • 相关阅读:
    Java里if...else语句
    Java里for循环和增强版循环;break语句:跳出循环;continue:跳转循环
    Java里while循环和do...while循环
    static 修饰符
    Java类变量(静态变量)
    Java实例变量
    Java局部变量
    Java访问实例变量和调用成员方法
    Java里import语句使用
    声明式服务调用
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1348961.html
Copyright © 2020-2023  润新知