0

Hi all I have a weird optimization question, here is the code I have changed some names for simplicity

CollectionObject mycollobj = new CollectionObject();
List<string> MyProducts = new List<string>();
//get collection of selected customers that were passed in
var chckedValues = form.GetValues("assignChkBx");
foreach(string customer in chckedValues )
{
    MyProducts.Clear();
    //MyProducts is then set to a data access method in my data access class
    MyProducts = DataLayerClass.GetProductsFromCustomer(customer);
    foreach(string product in MyProducts)
    {
       string item1 = DataLayerClass.GetItem1(product);
       string item2 = DataLayerClass.GetItem2(product);
       mycollobj.loaditems(item1, item2);
    }
}

Essentially mycollobj is a black box that is used for some fairly involved analysis (that I have no control over). Is there any better way to run this nested algorithm? Any suggestions are valued and please ask if you need clarification on anything. Thanks!

6
  • FYI this all works, its just slow Commented Oct 4, 2012 at 20:09
  • If mycollobj.loaditems is the slow part, can you move that call to a background thread? Commented Oct 4, 2012 at 20:11
  • I'm not actually sure what is slow, the whole process is slow (still a beginner at this stuff) Commented Oct 4, 2012 at 20:13
  • How is the DataLayerClass implemented? Specifically, when does it actually retrieve the data from the database? Commented Oct 4, 2012 at 20:13
  • 1
    Tried profiling to see what's slow? Commented Oct 4, 2012 at 20:13

1 Answer 1

3

Yes, this line: MyProducts = DataLayerClass.GetProductsFromCustomer(customer); is going to slow things down (a database call for every customer), also the nested DataLayerClass.GetItem1()/GetItem2 is making things much worse. Rather send down all the checkedValues to the database and return a Lookup with the customer and a Tuple containing item1 and item2:

ILookup<Customer, Tuple<string, string>> customerProducts = 
 DataLayerClass.GetCustomersWithProducts(chckedValues);

In short terms, move the logic to a single database query.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure where this would go in the code, do we nest it inside the first loop?
also there is no customer object, so I am a bit confused
No this would be done outside any loop. And than loop the result to call mycollobj.loaditems(item1, item2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.