What's the fastest way to read a text file line-by-line?

前端 未结 8 2197
情歌与酒
情歌与酒 2020-11-22 02:51

I want to read a text file line by line. I wanted to know if I\'m doing it as efficiently as possible within the .NET C# scope of things.

This is what I\'m trying so

8条回答
  •  天涯浪人
    2020-11-22 03:28

    While File.ReadAllLines() is one of the simplest ways to read a file, it is also one of the slowest.

    If you're just wanting to read lines in a file without doing much, according to these benchmarks, the fastest way to read a file is the age old method of:

    using (StreamReader sr = File.OpenText(fileName))
    {
            string s = String.Empty;
            while ((s = sr.ReadLine()) != null)
            {
                   //do minimal amount of work here
            }
    }
    

    However, if you have to do a lot with each line, then this article concludes that the best way is the following (and it's faster to pre-allocate a string[] if you know how many lines you're going to read) :

    AllLines = new string[MAX]; //only allocate memory here
    
    using (StreamReader sr = File.OpenText(fileName))
    {
            int x = 0;
            while (!sr.EndOfStream)
            {
                   AllLines[x] = sr.ReadLine();
                   x += 1;
            }
    } //Finished. Close the file
    
    //Now parallel process each line in the file
    Parallel.For(0, AllLines.Length, x =>
    {
        DoYourStuff(AllLines[x]); //do your work here
    });
    

提交回复
热议问题