开发者

Nested foreach loop slow

开发者 https://www.devze.com 2023-04-11 17:08 出处:网络
The following piece of code achives the desired results, but performance is extremely slow: SearchResultCollection absaUsers = ABSAds.FindAll();

The following piece of code achives the desired results, but performance is extremely slow:

SearchResultCollection absaUsers = ABSAds.FindAll();
SearchResultCollection srcUsers = ds.FindAll();

foreach (SearchResult users in srcUsers)
{
    string cn = users.Properties["cn"][0].ToString();
    string sn = users.Properties["sn"][0].ToString();
    string userID = users.Properties["uid"][0].ToString();
    string description = users.Properties["PersonnelAreaDesc"][0].ToString();
    string jobCodeID = users.Properties["JobcodeID"][0].ToString();
    string CostCentreID = users.Properties["costCentreID"][0].ToString();
    string CostCentreDescription = users.Properties["CostCentreDescription"][0].ToString();
    string givenName = users.Properties["givenName"][0].ToString();
    string employmentStatus = users.Properties["EmploymentStatus"][0].ToString();
    string EmploymentStatusDescription = users.Properties["EmploymentStatusDescription"][0].ToString();

    foreach (SearchResult absaUser in absaUsers)
    {

        string absaUID = absaUser.Properties["uid"][0].ToString();
        string absaEmploymentStatus = absaUser.Properties["EmploymentStatus"][0].ToString();
        string absaEmploymentStatusDescription = absaUser.Properties["EmploymentStatusDescription"][0].ToString();
        string absaEmployeeNumber = absaUser.Properties["employeeNumber"][0].ToString();

        if (absaUID == cn && absaEmployeeNumber==userID)
        {
            Console.WriteLine("User Record Found:" + cn);
            开发者_Python百科sw.WriteLine("Modify" + "," + cn + "," + description + "," + userID + "," + givenName + "," + sn + "," + jobCodeID + "," + CostCentreID + "," + CostCentreDescription + "," + sn + "," + cn + "," + employmentStatus + "," + EmploymentStatusDescription);
            sw.Flush();
            break;
        }
    }
}

It loops through 2 collections and mtaches the outer loops attributes with the inner's. Any suggestions on how I can optimise the performance?


It would be faster if you extracted all the absaUID values to a lookup first:

var lookup = absaUsers.Cast<SearchResult>()
                      .ToLookup(x => x.Properties["uid"][0].ToString());

Then you can just use:

foreach (SearchResult users in srcUsers)
{
    string cn = users.Properties["cn"][0].ToString();
    foreach (SearchResult matches in lookup[cn])
    {
        ...
    }
}

You haven't shown how absaUsers is defined - if it's a LINQ query expression, then it could be that your existing code will be going to the database on every iteration at the moment - whereas the above won't. On the other hand, if srcUsers is also a LINQ query expression talking to a database, you should consider doing all the matching at the database using a join instead.


you could use LINQ join, some examples are here, I'm assuming whoever built it into .NET found a pretty optimal way of doing that, and then loop through that. on a sidenote: what are your collection types? please add their declaration to the code snippet.


Use Lamda expressions:

Below is the sample one , You can optizime this to another level.

    List<SearchResult> allResultGroups=new  List<SearchResult>();
    foreach (SearchResult absaUser in absaUsers)
    {
    resultGroups = srcUsers.Where(g => g.cn == absaUser.absaUID && absaUser.absaEmployeeNumber==g.userID ).ToList();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消