using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Unique; namespace ConsoleApplication4 { class Data { public int this[int index] { get { return target2[index]; } } static int length = 1024 * 1024 * 10; byte[] target2 = new byte[length]; } class Program { static void Main(string[] args) { HPTimer.Init(); int length = 1024 * 1024 * 10; List<byte> target1 = new List<byte>(length); byte[] target2 = new byte[length]; Data target3 = new Data(); for (int i = 0; i < length; ++i ) { target1.Add(1); } { int a = 0; using (TimeTracker.Record("raw index access, list, 10M data")) { for (int i = 0; i < length; ++i) { a = target1[i]; } } ++a; } { int a = 0; using (TimeTracker.Record("IEnumerator, list, 10M data")) { for (var e = target1.GetEnumerator(); e.MoveNext(); ) { a = e.Current; } } ++a; } { int a = 0; using (TimeTracker.Record("raw index access, array, 10M data")) { for (int i = 0; i < length; ++i) { a = target2[i]; } } ++a; } { int a = 0; using (TimeTracker.Record("raw index access, .Length count, array, 10M data")) { for (int i = 0; i < target2.Length; ++i) { a = target2[i]; } } ++a; } { int a = 0; using (TimeTracker.Record("raw index access, ic, array, 10M data")) { for (int i = 0, ic = target2.Length; i < ic; ++i) { a = target2[i]; } } ++a; } { int a = 0; using (TimeTracker.Record("override index access, array, 10M data")) { for (int i = 0; i < length; ++i) { a = target3[i]; } } ++a; } Console.WriteLine(TimeTracker.Dump()); Console.Read(); } } }
raw index access, list, 10M data[1]: 40.54573, avg:40.54573
raw index access, ic, array, 10M data[1]: 25.45351, avg:25.45351
raw index access, .Length count, array, 10M data[1]: 24.81406, avg:24.81406
raw index access, array, 10M data[1]: 28.06017, avg:28.06017
IEnumerator, list, 10M data[1]: 39.3427, avg:39.3427
override index access, array, 10M data[1]: 42.73841, avg:42.73841
* note by kun 2016.12.18
* so best way to iterate cells array is return the array directly and use .Length for max count;