Search code examples
c#filefilterstreamreaderstreamwriter

the faster way to filter all emails end with a special caractere in a big text file using C#


i want a very faster way to filter all emails that end with a special caracter in a big text file (more than 200MB) using c#

var lines = File.ReadAllLines(file path);

foreach(var line in lines)
{
  if (line.EndsWith(myWord))
  {
    outputEmails.Text += line + Environment.NewLine;
  }
}

this code is very slow to achieve my goal.


Solution

  • Use File.ReadLines:

    foreach (var line in File.ReadLines("file path"))
    {
        if (line.EndsWith(myWord))
        {
            outputEmails.Text += line + Environment.NewLine;
        }
    }
    

    Internally it uses StreamReader so you don't load the entire file into memory before reading it.


    Even better performance may be achieved with memory mapped files. This saves extra memory copy operations when reading file contents.

    string line;
    var stringBuilder = new StringBuilder();
    using (var memoryMappedFile = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(@"D:\Temp\test.txt"))
    using (var viewStream = memoryMappedFile.CreateViewStream())
    using (var streamReader = new StreamReader(viewStream))
    {
        while ((line = streamReader.ReadLine()) != null)
            if (line.EndsWith(myWord))
                stringBuilder.AppendLine(line);
    }
    outputEmails.Text = stringBuilder.ToString();
    

    And yes, as the other answers say, using StringBuilder instead to raw string concatenation will also make code faster, especially when many lines match filter condition.