how to simplify the loop?
-
Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?
List rangeDataMappingsToClear= new List();
Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();foreach (string key in rangeDataMappingsToClear)
{
rangeDataMappings.Remove(key);
foreach (List<string> formulaKeys in rangeKeyMappings.Values)
{
while (formulaKeys.Remove(key)) { }
}
}
rangeDataMappingsToClear.Clear(); -
Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?
List rangeDataMappingsToClear= new List();
Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();foreach (string key in rangeDataMappingsToClear)
{
rangeDataMappings.Remove(key);
foreach (List<string> formulaKeys in rangeKeyMappings.Values)
{
while (formulaKeys.Remove(key)) { }
}
}
rangeDataMappingsToClear.Clear();you seem to need two nested loops, nothing can remedy that, unless your problem would allow for a different data structure altogether. However, if you have 100K rangeDataMappingsToClear elements, and only 10K rangeKeyMappings elements, that would indicate rangeDataMappingsToClear holds lots of duplicates; the first step would be to simplify rangeDataMappingsToClear, eliminating the duplicates. Either make sure you don't cause duplicates (that is the cheapest solution), or create a new collection holding all the distinct elements just once; a Dictionary or HashSet could simplify this step. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?
List rangeDataMappingsToClear= new List();
Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();foreach (string key in rangeDataMappingsToClear)
{
rangeDataMappings.Remove(key);
foreach (List<string> formulaKeys in rangeKeyMappings.Values)
{
while (formulaKeys.Remove(key)) { }
}
}
rangeDataMappingsToClear.Clear(); -
you seem to need two nested loops, nothing can remedy that, unless your problem would allow for a different data structure altogether. However, if you have 100K rangeDataMappingsToClear elements, and only 10K rangeKeyMappings elements, that would indicate rangeDataMappingsToClear holds lots of duplicates; the first step would be to simplify rangeDataMappingsToClear, eliminating the duplicates. Either make sure you don't cause duplicates (that is the cheapest solution), or create a new collection holding all the distinct elements just once; a Dictionary or HashSet could simplify this step. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
As Luc has alluded to, if one of the issues is that the rangeDataMappingsToClear list does contain a lot of duplicates, a quick and dirty fix could be to use the Distinct method
foreach (string key in rangeDataMappingsToClear.Distinct())
"You get that on the big jobs."
-
Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?
List rangeDataMappingsToClear= new List();
Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();foreach (string key in rangeDataMappingsToClear)
{
rangeDataMappings.Remove(key);
foreach (List<string> formulaKeys in rangeKeyMappings.Values)
{
while (formulaKeys.Remove(key)) { }
}
}
rangeDataMappingsToClear.Clear();Do you have to use List? Or can you use Hashset?