2014-10-30 1 views
0

내가 객체의 네 개의 컬렉션이 있습니다큰 개체 모음에서 새 개체 모음을 빠르게 만드는 방법은 무엇입니까?

public static ConcurrentBag<string> ProductCodes; 
public static ConcurrentDictionary<string, Product> ProductDictionary; 
public static ConcurrentBag<IIMIM00> iimiml; // 37782 items 
public static ConcurrentBag<IIMAR00> iimarl; // 73516 items 
public static ConcurrentBag<PRODUCTINF> additionall; // 6238 items 
public static ConcurrentBag<PF_COSSTO> ProductsAndCosts; // 862096 items 

나는에서 새로운 '제품'개체를 만들려면 제품 코드의 고유 목록을 얻을 모든 그래서 첫째 :

Parallel.ForEach(ProductsAndCosts, i => ProductCodes.Add(i.improd)); 
ProductCodes = new ConcurrentBag<string>(ProductCodes.Distinct().ToList()) 

제품 클래스 :

public class Product 
{ 
    public IIMIM00 iimim { get; set; } 
    public List<IIMAR00> iimar { get; set; } 
    public PRODUCTINF productAdditional { get; set; } 
} 

정렬하고 제품 코드 및 제품 객체 사전을 만들 수있는 나의 일상 :

Parallel.ForEach(ProductCodes, SortandCreate);  

public static void SortandCreate(string productCode) 
{ 
    var product = new Product {iimim = iimiml.Single(x => x.IMPROD.Equals(productCode))}; 
    try 
    { 
     product.iimar = iimarl.Where(x => x.ARPROD.Equals(productCode)).ToList(); 
    } 
    catch (Exception) 
    { 
     product.iimar = new List<IIMAR00>(); 
    } 
    try 
    { 
     product.productAdditional = additionall.Single(x => x.PRODUCTCOD.Equals(productCode)); 
    } 
    catch (Exception) 
    { 
     product.productAdditional = new PRODUCTINF(); 
    } 

    ProductDictionary.TryAdd(productCode, product); 
} 

Product 개체가 항상 IIMAR00 또는 PRODUCTINF 인스턴스를 갖지 않으므로 try 캐치가 있습니다.

해결 방법은 매우 느립니다. i5에서 2:30 이상 걸립니다. 이 문제를 해결할 더 좋은 방법이 있는지 확실하지 않습니다.

답변

0

처리 예외를 처리하는 데 많은 시간이 걸리기 때문에 프로그램 흐름에는 try-catch을 사용하지 마십시오. 사실 은밀한 if-else입니다. 단순히 null 체크를 추가하면 예외 처리에 의해 손실 된 시간을 얻게됩니다 :

product.iimar = iimarl.Where(x => x.ARPROD != null 
           && x.ARPROD.Equals(productCode)) 
         .ToList(); 
관련 문제