I have a function that loops through an array of strings containing approximately 3 million lines of strings. My problem is this loop is running too slowly and I want to know how to make it faster.
string[] words = { "Hello", "world", "!", "My", "name", "is", "Something", "."};
List<MyClass> result = new List<MyClass>();
for (int i = 0; i < words.Length; i++)
{
if (words[i] == "&&&")
{ }
else if (words.Length > i + 2 && FileContains(words[i] + " " + words[i + 1] + " " + words[i + 2]))
{
result.Add(new MyClass(words[i] + " " + words[i + 1] + " " + words[i + 2]));
words[i + 1] = "&&&";
words[i + 2] = "&&&";
}
else if (words.Length > i + 1 && FileContains(words[i] + " " + words[i + 1]))
{
result.Add(new MyClass(words[i] + " " + words[i + 1]));
words[i + 1] = "&&&";
}
else
result.Add(new MyClass(words[i], Parent));
}
And my FileContains
function which loops through the array of strings:
public static bool FileContains(string Text)
{
foreach (string line in ARRAYOFWORDS) // Approximately 3 million strings
{
if (Text.ToLower() == line.Substring(0, line.LastIndexOf('|')))
{
return true;
}
}
return false;
}