// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Text; namespace E { public class MainClass { public static void TestStd() { StreamReader sr = new StreamReader(@"C:\avuarc.ext"); DateTime t1 = DateTime.Now; string line; int n = 0; while((line = sr.ReadLine()) != null) { n++; } DateTime t2 = DateTime.Now; GC.Collect(); Console.WriteLine("one line: " + n + " lines " + (t2 - t1).TotalSeconds + " secs " + GC.GetTotalMemory(false)/1000000 + " MB"); sr.Close(); } public static void TestOne() { StreamReader sr = new StreamReader(@"C:\avuarc.ext"); DateTime t1 = DateTime.Now; string all = sr.ReadToEnd(); string[] lines = all.Split("\r\n".ToCharArray()); int n = lines.Length/2; // <---- hack because we get a lot of fake empty lines DateTime t2 = DateTime.Now; GC.Collect(); Console.WriteLine("all and split: " + n + " lines " + (t2 - t1).TotalSeconds + " secs " + GC.GetTotalMemory(false)/1000000 + " MB"); sr.Close(); } public static void TestBuf() { StreamReader sr = new StreamReader(@"C:\avuarc.ext", Encoding.Default, false, 100000); DateTime t1 = DateTime.Now; string line; int n = 0; while((line = sr.ReadLine()) != null) { n++; } DateTime t2 = DateTime.Now; GC.Collect(); Console.WriteLine("one line with buffer: " + n + " lines " + (t2 - t1).TotalSeconds + " secs " + GC.GetTotalMemory(false)/1000000 + " MB"); sr.Close(); } public static void Main(string[] args) { TestStd(); TestOne(); TestBuf(); TestStd(); TestOne(); TestBuf(); TestStd(); TestOne(); TestBuf(); Console.ReadLine(); } } }