I’ve got a nested foreach loop that I really need to cut the computation time on. Each collection is at about 50 members, so the extrapolation is huge. I’ve looked at a lot of information about SelectMany, but I’m still not entirely sure how to use it, or if it’s the correct solution.
List<string> StringList; //Populated in previous code
Type[] assemblyTypes = RandomAssembly.GetTypes();
foreach (String name in StringList)
{
foreach (Type at in assemblyTypes)
{
if (name == at.Name)
{
//Do stuff.
}
}
}
Thanks in advance!
Use a lookup (such as a dictionary) to increase the speed of checking for a type name:
You should also check which of the 2 collections (
StringListorassemblyTypes) is likely to be larger. You generally want the larger one to be converted to the lookup in order to reduce the number of iterations.