locked
Nested loops, performance issue. RRS feed

  • Question

  • Hi,

    I was asked to work on performance issue one of our asp.net pages.  I tracked down the problem but I am having a hard time to implement it. The problem is the nested foreach loops.  Inner loop has 1000 iterations when the code runs. I have to take inner nested loop to outside, making them sparate foreach. How should I do it?

    Here is the code:

    private void LoadPlanList(string planCategory)

            {

                var unfilteredPlanList = FldrDBAccess.GetPlansByCategory(planCategory);

                var finalPlanList = new List<Plan>();

                //  For New Set up - Plan List is empty

                if (unfilteredPlanList != null)

                {

                    // IF "CTS" member, no filtering is needed, else filter list according to org_entity

                    if (org_Group == GetUserProfile.Org_Group_CTS)

                        finalPlanList = unfilteredPlanList;

                    else

                    {

                        // Get my hierachy, and filter plan list accordingly

                        var myHierarchy = TaxonomyHelper.GetAllParents(SPContext.Current.Site, org_Entity).ToString() + org_Entity;

                        bool match = true;

                        foreach (var plan in unfilteredPlanList)

                        {

                            match = true;

                            var assignmentList = FldrDBAccess.GetAllEntitylListByPlanID(plan.ID);

                            //  If unassigend, only CTS member can update

                            if (assignmentList == null)

                                continue;

                            foreach (string entiy in assignmentList)

                            {

                                var hospHierarchy = TaxonomyHelper.GetAllParents(SPContext.Current.Site, entiy).ToString();

                                // Skip to next plan on first no-match

                                if (!hospHierarchy.Contains(myHierarchy) &&

                                    TaxonomyHelper.GetTermsByLabel(SPContext.Current.Site,entiy)  != null) // Depreacated terms return null

                                {

                                    match = false;

                                    break;

                                }

                            }

                            if (match)

                                finalPlanList.Add(plan);

                        }

                    }

                }

                selectPlanList.DataTextField = "Title";

                selectPlanList.DataValueField = "ID";

                selectPlanList.DataSource = finalPlanList;

                selectPlanList.DataBind();

            }


    • Edited by koppele Saturday, September 15, 2012 10:21 PM
    Saturday, September 15, 2012 10:19 PM

Answers

  • why cant you apply select?

    do you have a "using System.Linq;" statement at the top of the file?


    gimme some slamming techno!!!!

    • Marked as answer by koppele Monday, September 17, 2012 3:48 AM
    Sunday, September 16, 2012 6:26 AM

All replies

  • take a look at my response to your regex question over here:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6d26a711-20b6-4053-9bae-e113e6d747de


    gimme some slamming techno!!!!

    Saturday, September 15, 2012 10:44 PM
  • ok but i cannot apply select on "finalPlanList" . it is a generic list.

    private List<Plan> GetPlans(string planCategory)
    {
       
    var finalPlanList = FldrDBAccess.GetPlansByCategory(planCategory);
       
    if (org_Group != GetUserProfile.Org_Group_CTS)
       
    {
           
    var myHierarchy = TaxonomyHelper.GetAllParents(SPContext.Current.Site, org_Entity).ToString() + org_Entity;
           
    var temp = finalPlanList
               
    .Select(plan => new { Plan = plan, Assignments = FldrDBAccess.GetAllEntitylListByPlanID(plan.ID) })
               
    .Where(pa =>
                        pa
    .Assignments != null
                       
    && pa.Assignments.Any(assn => TaxonomyHelper.GetAllParents(SPContext.Current.Site, assn).Contains(myHierarchy)
                           
    && TaxonomyHelper.GetTermsByLabel(SPContext.Current.Site, assn) != null))
                           
    .Select(pa => pa.Plan).ToArray();
            finalPlanList
    .AddRange(temp);
       
    }
       
    return finalPlanList;

    Sunday, September 16, 2012 6:15 AM
  • why cant you apply select?

    do you have a "using System.Linq;" statement at the top of the file?


    gimme some slamming techno!!!!

    • Marked as answer by koppele Monday, September 17, 2012 3:48 AM
    Sunday, September 16, 2012 6:26 AM
  • Yeah, I forgot to include using statement.  I used your code and it worked but I was still slow. I found the problem so it was not the nested loop. Your code helped me to see that nested loop was not the problem so thank you very much.

    Monday, September 17, 2012 3:48 AM
  • Glad I could help.

    What was the issue?


    gimme some slamming techno!!!!

    Monday, September 17, 2012 4:38 AM
  • I am a SharePoint developer. The code is for SP web part and it connects to Term Store which is service application and you connect to it by web services.  That is I beleive :)  The foreach code was connecting to it 1000 times.   I cached the information into a dictinoary then use it instead of connecting so many times.

    thank you 

    Monday, September 17, 2012 6:12 AM