1

i have this loop and it loops for large count like 30 000 times at least
i am looking for some way to improve it's performance
DbRecordDictionary is derived from DictionaryBase class
here is the loop:

ArrayList noEnter = new ArrayList();
DbRecordDictionary oldArray = new DbRecordDictionary();
DbRecordDictionary userArray = new DbRecordDictionary();
DbRecordDictionary result = null;
foreach (string key in keys)
{
    if (noEnter.Contains(key))
    { //may need cast!
        if (count < 1)
            result.Add(key, userArray[key]);
        else if (oldArray.Count == 0)
            break;
        else if (oldArray.Contains(key))
            result.Add(key, userArray[key]);
    }                
}
5
  • Please fix the formatting. Don't use `. Just indent each line by at least four spaces. Commented Nov 25, 2010 at 10:27
  • Don't use <code> tags. They don't work. Select your code and press the 010101 button instead Commented Nov 25, 2010 at 10:27
  • 2
    you might consider including noEnter, userArray, oldArray declarations.. Commented Nov 25, 2010 at 10:30
  • 1
    @Armen: can you believe I dumbly started scanning my keyboard for this mythical 010101 button? Commented Nov 25, 2010 at 10:31
  • noEnter is array list and the rest are derived from Dictionarybase class Commented Nov 25, 2010 at 10:39

5 Answers 5

2

You may want to use a Dictionary/Hashset for oldArray, but else there is not much you can do. Also noEnter if that is an array.

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

Comments

1

From what I can see, the variable count or the oldArray never changes during the loop, so you can place those condition outside the loop, and make two different loops.

if (count < 1) {
  foreach (string key in keys) {
    if (noEnter.Contains(key)) {
      result.Add(key, userArray[key]);
    }
  }
} else if (oldArray.Count == 0) {
  // no data
} else {
  foreach (string key in keys) {
    if (noEnter.Contains(key)) {
      if (oldArray.Contains(key)) {
        result.Add(key, userArray[key]);
      }
    }                
  }
}

The collections noEnter and oldArray should be dictionaries, otherwise you will be spending a lot of execution time in the Contains calls.

Comments

1

If noEnter has more then about 10 items in it, then use a Dictionary rathern then a List/Array for it. As a Dictionary can look up a item without having to look at all the items, when an List/Array has to loop over all items.

Otherwise consider shorting "keys" and "oldArray" and then proforming a "merge" on them. Look at the code for a "merge sort" to see how to do the merge. (This would need carefull profiling)

Comments

0

try this

(from k in keys
     where noEnter.Contains(k) &&
           oldArray.Count > 0 &&
           count < 1 &&
           oldArray.Contains(k)
     select k)
        .ToList()
        .ForEach(k => result.Add(k, userArray[k]));

Comments

0

Small small optimization could be using generics ArrayList noEnter = new ArrayList(); would be List noEnter = new List(); and DbRecordDictionary would inherit Dictonary instead of DictionaryBase.

im not 100% sure that you would gain performance but you will use a more modern c# style.

Comments

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.