// 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.Collections.Generic; namespace E { public class LoopOpt { private const int NELM = 10000; private const int NREP = 1000000; public static void Main(string[] args) { List lst = new List(); for(int i = 0; i < NELM; i++) { lst.Add(i); } DateTime dt1 = DateTime.Now; for(int i = 0; i < NREP; i++) { for(int j = 0; j < lst.Count; j++) { if(lst[j] != j) { throw new Exception("Ooops"); } } } DateTime dt2 = DateTime.Now; Console.WriteLine((dt2 - dt1).TotalSeconds); DateTime dt3 = DateTime.Now; for(int i = 0; i < NREP; i++) { int n = lst.Count; for(int j = 0; j < n; j++) { if(lst[j] != j) { throw new Exception("Ooops"); } } } DateTime dt4 = DateTime.Now; Console.WriteLine((dt4 - dt3).TotalSeconds); } } }