// arraytest.cs -- a demonstration of various ways you might process an IList array // © 2006 Microsoft Corporation // This program is offered AS IS with no warranty implied and confers no rights. // Also please don't run around the house while holding scissors; thank you. using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace ArrayTest { class Program { class Test { static public int Sum(IList indices) { int result = 0; for (int i = 0; i < indices.Count; i++) result += indices[i]; return result; } static public int SumForeach(IList indices) { int result = 0; foreach (ushort us in indices) result += us; return result; } static public int SumSpecial(IList indices) { int result = 0; ushort[] arr = indices as ushort[]; if (arr != null) { foreach (ushort us in arr) result += us; } else { foreach (ushort us in indices) result += us; } return result; } } class ArrayWrapper : IList { private T[] arr; public ArrayWrapper(T[] _arr) { arr = _arr; } T IList.this[int index] { get { return arr[index]; } set { throw new System.NotImplementedException(); } } private void NYI() { throw new System.NotImplementedException(); } int IList.IndexOf(T item) { NYI(); return -1;} void IList.Insert(int index, T item) { NYI(); } void IList.RemoveAt(int index) { NYI(); } int ICollection.Count { get { return arr.Length; } } bool ICollection.IsReadOnly { get { return true; } } void ICollection.Add(T item) { NYI(); } void ICollection.Clear() { NYI(); } bool ICollection.Contains(T item) { NYI(); return false;} void ICollection.CopyTo(T[] array, int arrayIndex) { NYI(); } bool ICollection.Remove(T item) { NYI(); return false;} IEnumerator IEnumerable.GetEnumerator() { NYI(); return null;} IEnumerator IEnumerable.GetEnumerator() { NYI(); return null;} } static void Main(string[] args) { int iterations = 500000; Test test = new Test(); ushort[] arr = new ushort[iterations]; List lst = new List(iterations); for (int i = 0; i < iterations; i++) lst.Add(0); ArrayWrapper aw = new ArrayWrapper(new ushort[iterations]); Test1(arr); Test2(lst); Test3(aw); Test4(arr); Test5(lst); Test6(arr); Test7(lst); } static void Test1(ushort[] arr) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.Sum(arr); sw.Stop(); Console.WriteLine("Elapsed Milliseconds Array: {0}", sw.ElapsedMilliseconds); } static void Test2(List lst) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.Sum(lst); sw.Stop(); Console.WriteLine("Elapsed Milliseconds List<>: {0}", sw.ElapsedMilliseconds); } static void Test3(ArrayWrapper aw) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.Sum(aw); sw.Stop(); Console.WriteLine("Elapsed Milliseconds ArrayWrapper: {0}", sw.ElapsedMilliseconds); } static void Test4(ushort[] arr) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.SumForeach(arr); sw.Stop(); Console.WriteLine("Elapsed Milliseconds Array via foreach: {0}", sw.ElapsedMilliseconds); } static void Test5(List lst) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.SumForeach(lst); sw.Stop(); Console.WriteLine("Elapsed Milliseconds List<> via foreach: {0}", sw.ElapsedMilliseconds); } static void Test6(ushort[] arr) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.SumSpecial(arr); sw.Stop(); Console.WriteLine("Elapsed Milliseconds Array via special: {0}", sw.ElapsedMilliseconds); } static void Test7(List lst) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Test.SumSpecial(lst); sw.Stop(); Console.WriteLine("Elapsed Milliseconds List<> via special: {0}", sw.ElapsedMilliseconds); } } }