Below is the method that I have written for reading from a text file. While reading, I need to match line string to given regex and if it matches then I need to add the line string to a collection.
private static void GetOrigionalRGBColours(string txtFile)
{
string tempLineValue;
Regex regex = new Regex(@"^\d+.?\d* \d+.?\d* \d+.?\d* SRGB$");
using (StreamReader inputReader = new StreamReader(txtFile))
{
while (null != (tempLineValue = inputReader.ReadLine()))
{
if (regex.Match(tempLineValue).Success
&& tempLineValue != "1 1 1 SRGB"
&& tempLineValue != "0 0 0 SRGB")
{
string[] rgbArray = tempLineValue.Split(' ');
RGBColour rgbColour = new RGBColour() { Red = Convert.ToDecimal(rgbArray[0]), Green = Convert.ToDecimal(rgbArray[1]), Blue = Convert.ToDecimal(rgbArray[2]) };
originalColourList.Add(rgbColour);
}
}
}
}
When this method is run for a text file of 4MB
having 28653 lines, it takes around 3 minutes just to finish the above method. Also, as a result of the above run, originalColourList
is populated with 582 items.
Can anyone please guide on how can I improve the performance of this method? I had truncated the original text file for testing purpose. The actually text file size may go up to 60MB
.
Match
to about half! Definitely worth doing if you plan to use the same regexp a lot. – Andris Jan 31 at 7:08RegexOptions.Compiled
payed for itself after about 10000Match
es. – Andris Jan 31 at 7:11