static void Main() { const int REPEATS = 50; const int SIZE = 1000000; List <int> list = new List <int>(SIZE); Random rand = new Random(12345); for (int i = 0; i < SIZE; i++) { list.Add(rand.Next(5000)); } int[] arr = list.ToArray(); DynamicArray <int> darr = new DynamicArray <int>(); foreach (int v in list) { darr.Add(v); } long hits = 0; long chk = 0; Stopwatch watch = Stopwatch.StartNew(); for (int rpt = 0; rpt < REPEATS; rpt++) { int len = list.Count; for (int i = 0; i < len; i++) { chk += list[i]; hits++; } } watch.Stop(); Console.WriteLine("List/for : {0}ms ({1}) ", watch.ElapsedMilliseconds, chk); chk = 0; hits = 0; watch = Stopwatch.StartNew(); for (int rpt = 0; rpt < REPEATS; rpt++) { for (int i = 0; i < arr.Length; i++) { chk += arr[i]; hits++; } } watch.Stop(); Console.WriteLine("Array/for : {0}ms ({1})", watch.ElapsedMilliseconds, chk); chk = 0; hits = 0; watch = Stopwatch.StartNew(); int[] arr2 = darr.Compact(); for (int rpt = 0; rpt < REPEATS; rpt++) { for (int i = 0; i < arr2.Length; i++) { chk += arr2[i]; hits++; } } watch.Stop(); Console.WriteLine("DArray/for : {0}ms ({1})", watch.ElapsedMilliseconds, chk); Console.WriteLine(); chk = 0; watch = Stopwatch.StartNew(); for (int rpt = 0; rpt < REPEATS; rpt++) { foreach (int i in list) { chk += i; } } watch.Stop(); Console.WriteLine("List/foreach : {0}ms ({1})", watch.ElapsedMilliseconds, chk); chk = 0; watch = Stopwatch.StartNew(); for (int rpt = 0; rpt < REPEATS; rpt++) { foreach (int i in arr) { chk += i; } } watch.Stop(); Console.WriteLine("Array/foreach : {0}ms ({1})", watch.ElapsedMilliseconds, chk); chk = 0; watch = Stopwatch.StartNew(); int[] c = darr.Compact(); for (int rpt = 0; rpt < REPEATS; rpt++) { foreach (int i in c) { chk += i; } } watch.Stop(); Console.WriteLine("DArray/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk); Console.WriteLine(); Console.WriteLine("Total number of hits: {0:N0}", hits); Console.ReadKey(); }