.NET Framework 4.0 Newbie : File.ReadLines

.NET Framework 4.0 Newbie : File.ReadLines

  • Comments 1

In .NET Framework 4.0 instead of you using File.ReadAllLines() use ReadLines() to get a better performance. Question is why? File.ReadLines() uses IEnumerable to get the lines. Unlike File.ReadAllLines() it does not first read full files and the starts reading if you have implemented the iteration. Also File.ReadAllLines() does not implement MoveNext().

 

But there is a catch, if you test your application to compare through sampling you may find File.ReadAllLines() is performing faster. As reading a bigger chunk at a time is faster that reading line by line. Also File.ReadLines() does not read anything unless you iterate it. So the time span between opening a file and start reading is actually nominal compared to File.ReadAllLines().

 

Use File.ReadLines() is always better and consider it for VERY large files.

 

Interesting observation

 

static void Main(string[] args)

{

    Stopwatch sw = new Stopwatch();

 

    sw.Start();

    CallingReadLines();

 

    //CallingReadAllLines();

    sw.Stop();

    string sTime1 = sw.ElapsedMilliseconds.ToString();

 

    Console.WriteLine("Time taken in Milliseconds : {0}", sw.ElapsedMilliseconds.ToString() );

       

}

 

 

private static void CallingReadAllLines()

{

    string[] data = File.ReadAllLines(@"D:\Temp\LargeFile2.txt");

 

    foreach (var line in data)

    {

        //Console.Write(".");

        //Console.WriteLine(line);

        File.AppendAllText(@"C:\Temp\Out_LargeFile1.txt", line);

    }

}

 

 

private static void CallingReadLines()

{

    IEnumerable<string> data = File.ReadLines(@"D:\Temp\LargeFile.txt");

 

    foreach (var line in data)

    {

        //Console.Write(".");

        //Console.WriteLine(line);

        File.AppendAllText(@"C:\Temp\Out_LargeFile2.txt", line);

    }

}

 

 

Namoskar!!!

 

Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
  • Yea... Not only interesting... also impressive observation.

    Thanks,

    Pranav Ainavolu

Page 1 of 1 (1 items)