// 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; using System.Collections.Generic; namespace E { public class MainClass { private const int REP = 100; private const int N = 1000000; private static string[] VALS = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; public static void Main(string[] args) { string[] sa = null; DateTime dt1 = DateTime.Now; for(int j = 0; j < REP; j++) { sa = new string[N]; for(int i = 0; i < N; i++) { sa[i] = VALS[i % VALS.Length]; } } DateTime dt12 = DateTime.Now; for(int j = 0; j < REP; j++) { for(int i = 0; i < N; i++) { if(sa[i] != VALS[i % VALS.Length]) { Console.WriteLine("Oops"); Environment.Exit(1); } } } DateTime dt2 = DateTime.Now; object[] oa = null; Console.WriteLine("{0,-15} : {1,6:f2} {2,6:f2}", "string array", (dt12 - dt1).TotalSeconds, (dt2 - dt12).TotalSeconds); DateTime dt3 = DateTime.Now; for(int j = 0; j < REP; j++) { oa = new object[N]; for(int i = 0; i < N; i++) { oa[i] = VALS[i % VALS.Length]; } } DateTime dt34 = DateTime.Now; for(int j = 0; j < REP; j++) { for(int i = 0; i < N; i++) { if((string)oa[i] != VALS[i % VALS.Length]) { Console.WriteLine("Oops"); Environment.Exit(1); } } } DateTime dt4 = DateTime.Now; Console.WriteLine("{0,-15} : {1,6:f2} {2,6:f2}", "object array", (dt34 - dt3).TotalSeconds, (dt4 - dt34).TotalSeconds); ArrayList al = null; DateTime dt5 = DateTime.Now; for(int j = 0; j < REP; j++) { al = new ArrayList(); for(int i = 0; i < N; i++) { al.Add(VALS[i % VALS.Length]); } } DateTime dt56 = DateTime.Now; for(int j = 0; j < REP; j++) { for(int i = 0; i < N; i++) { if((string)al[i] != VALS[i % VALS.Length]) { Console.WriteLine("Oops"); Environment.Exit(1); } } } DateTime dt6 = DateTime.Now; Console.WriteLine("{0,-15} : {1,6:f2} {2,6:f2}", "array list", (dt56 - dt5).TotalSeconds, (dt6 - dt56).TotalSeconds); List gl = null; DateTime dt7 = DateTime.Now; for(int j = 0; j < REP; j++) { gl = new List(); for(int i = 0; i < N; i++) { gl.Add(VALS[i % VALS.Length]); } } DateTime dt78 = DateTime.Now; for(int j = 0; j < REP; j++) { for(int i = 0; i < N; i++) { if(gl[i] != VALS[i % VALS.Length]) { Console.WriteLine("Oops"); Environment.Exit(1); } } } DateTime dt8 = DateTime.Now; Console.WriteLine("{0,-15} : {1,6:f2} {2,6:f2}", "generic list", (dt78 - dt7).TotalSeconds, (dt8 - dt78).TotalSeconds); } } }