Last active
August 29, 2015 13:55
-
-
Save hazzik/8727893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======== | |
Array.ForEach: 341.4, Min: 338, Max: 346 | |
Array for i != Length; ++i: 355.5, Min: 352, Max: 365 | |
Array for ++i: 356.8, Min: 350, Max: 381 | |
Array for: 397.9, Min: 391, Max: 420 | |
Array for i != Length: 410.6, Min: 398, Max: 468 | |
Array for, cached length: 413.5, Min: 399, Max: 502 | |
Array foreach: 437.9, Min: 433, Max: 447 | |
List<T>.ForEach: 489.7, Min: 480, Max: 540 | |
IList<T> for, cached length: 590.5, Min: 582, Max: 619 | |
List<T> for, cached length: 600.4, Min: 583, Max: 664 | |
List<T> foreach: 672.1, Min: 660, Max: 685 | |
List<T> for: 706.9, Min: 694, Max: 735 | |
List<T> for ++i: 707.3, Min: 696, Max: 729 | |
List<T> for i != Count: 709.5, Min: 696, Max: 765 | |
IList<T> for: 719.4, Min: 713, Max: 727 | |
IList<T> for i != Count: 719.5, Min: 712, Max: 739 | |
List<T> for i != Count; ++i: 728.4, Min: 701, Max: 883 | |
IList<T> for i != Count; ++i: 765.5, Min: 756, Max: 780 | |
IList<T> for ++i: 824.3, Min: 806, Max: 888 | |
IList<T> foreach: 928.4, Min: 916, Max: 948 | |
IEnumerable<T> foreach: 998.5, Min: 986, Max: 1026 | |
======== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======== | |
Array for: 175.3, Min: 171, Max: 178 | |
Array for ++i: 176.7, Min: 173, Max: 185 | |
Array for, cached length, ++i: 177.5, Min: 171, Max: 188 | |
Array for, cached length, i != Length; ++i: 177.5, Min: 170, Max: 196 | |
Array foreach: 179, Min: 172, Max: 189 | |
Array for i != Length; ++i: 179.2, Min: 171, Max: 189 | |
Array for i != Length: 203.1, Min: 198, Max: 207 | |
Array for, cached length: 206.1, Min: 200, Max: 215 | |
Array for, cached length, i != Length: 207.3, Min: 198, Max: 223 | |
List<T> for ++i: 259.5, Min: 254, Max: 269 | |
List<T> for i != Count; ++i: 260.8, Min: 253, Max: 277 | |
List<T> for, cached length: 260.9, Min: 257, Max: 274 | |
Array.ForEach: 261.7, Min: 254, Max: 287 | |
List<T> for i != Count: 262.7, Min: 253, Max: 293 | |
List<T> for, cached length, i != Count: 265.5, Min: 255, Max: 282 | |
List<T> for, cached length, i != Count; ++i: 266.4, Min: 254, Max: 297 | |
List<T> for, cached length, ++i: 267.2, Min: 255, Max: 304 | |
List<T> for: 273.4, Min: 255, Max: 385 | |
IList<T> for, cached length, ++i: 391.6, Min: 367, Max: 443 | |
List<T>.ForEach: 404.3, Min: 397, Max: 426 | |
IList<T> for, cached length, i != Count; ++i: 405.5, Min: 369, Max: 580 | |
IList<T> for, cached length: 406.5, Min: 394, Max: 423 | |
IList<T> for, cached length, i != Count: 421.5, Min: 395, Max: 517 | |
List<T> foreach: 438.3, Min: 424, Max: 461 | |
IList<T> for i != Count: 619.8, Min: 592, Max: 668 | |
IList<T> for ++i: 634.9, Min: 621, Max: 668 | |
IList<T> for i != Count; ++i: 644.6, Min: 620, Max: 692 | |
IList<T> for: 651.3, Min: 617, Max: 754 | |
IList<T> foreach: 858.8, Min: 821, Max: 952 | |
IEnumerable<T> foreach: 875.9, Min: 827, Max: 1065 | |
======== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Tests | |
{ | |
public static void TestFor(int[] array) | |
{ | |
for (int i = 0; i < array.Length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestFor(IList<int> array) | |
{ | |
for (int i = 0; i < array.Count; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestFor(List<int> array) | |
{ | |
for (int i = 0; i < array.Count; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForPrefix(int[] array) | |
{ | |
for (int i = 0; i < array.Length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForPrefix(IList<int> array) | |
{ | |
for (int i = 0; i < array.Count; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForPrefix(List<int> array) | |
{ | |
for (int i = 0; i < array.Count; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForNotEqual(int[] array) | |
{ | |
for (int i = 0; i != array.Length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForNotEqual(IList<int> array) | |
{ | |
for (int i = 0; i != array.Count; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForNotEqual(List<int> array) | |
{ | |
for (int i = 0; i != array.Count; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForNotEqualPrefix(int[] array) | |
{ | |
for (int i = 0; i != array.Length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForNotEqualPrefix(IList<int> array) | |
{ | |
for (int i = 0; i != array.Count; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForNotEqualPrefix(List<int> array) | |
{ | |
for (int i = 0; i != array.Count; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLength(int[] array) | |
{ | |
for (int i = 0, length = array.Length; i < length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLength(IList<int> array) | |
{ | |
for (int i = 0, length = array.Count; i < length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLength(List<int> array) | |
{ | |
for (int i = 0, length = array.Count; i < length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthPrefix(int[] array) | |
{ | |
for (int i = 0, length = array.Length; i < length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthPrefix(IList<int> array) | |
{ | |
for (int i = 0, length = array.Count; i < length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthPrefix(List<int> array) | |
{ | |
for (int i = 0, length = array.Count; i < length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthNotEqual(int[] array) | |
{ | |
for (int i = 0, length = array.Length; i != length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthNotEqual(IList<int> array) | |
{ | |
for (int i = 0, length = array.Count; i != length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthNotEqual(List<int> array) | |
{ | |
for (int i = 0, length = array.Count; i != length; i++) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthNotEqualPrefix(int[] array) | |
{ | |
for (int i = 0, length = array.Length; i != length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthNotEqualPrefix(IList<int> array) | |
{ | |
for (int i = 0, length = array.Count; i != length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForCachedLengthNotEqualPrefix(List<int> array) | |
{ | |
for (int i = 0, length = array.Count; i != length; ++i) | |
{ | |
Work(array[i]); | |
} | |
} | |
public static void TestForeach(int[] array) | |
{ | |
foreach (int e in array) | |
{ | |
Work(e); | |
} | |
} | |
public static void TestForeach(IList<int> array) | |
{ | |
foreach (int e in array) | |
{ | |
Work(e); | |
} | |
} | |
public static void TestForeach(List<int> array) | |
{ | |
foreach (var e in array) | |
{ | |
Work(e); | |
} | |
} | |
public static void TestForeach(IEnumerable<int> array) | |
{ | |
foreach (var e in array) | |
{ | |
Work(e); | |
} | |
} | |
public static void TestArrayForEach(int[] array) | |
{ | |
Array.ForEach(array, Work); | |
} | |
public static void TestListForEach(List<int> array) | |
{ | |
array.ForEach(Work); | |
} | |
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] | |
private static void Work(int i) | |
{ | |
} | |
private static readonly IDictionary<string, IList<long>> results = new Dictionary<string, IList<long>>(); | |
public static void Test(Action action, string name) | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
action(); | |
sw.Stop(); | |
IList<long> tries; | |
if (!results.TryGetValue(name, out tries)) | |
{ | |
results[name] = tries = new List<long>(); | |
} | |
tries.Add(sw.ElapsedMilliseconds); | |
Console.WriteLine(name + ": " + sw.ElapsedMilliseconds); | |
} | |
public static void Main() | |
{ | |
var array = Enumerable.Repeat(100, 100000000).ToArray(); | |
var list = new List<int>(array); | |
IList<int> ilist = list; | |
IEnumerable<int> ienumerable = list; | |
for (int i = 0; i < 10; i++) | |
{ | |
Test(() => TestFor(array), "Array for"); | |
Test(() => TestFor(list), "List<T> for"); | |
Test(() => TestFor(ilist), "IList<T> for"); | |
Test(() => TestForPrefix(array), "Array for ++i"); | |
Test(() => TestForPrefix(list), "List<T> for ++i"); | |
Test(() => TestForPrefix(ilist), "IList<T> for ++i"); | |
Test(() => TestForNotEqual(array), "Array for i != Length"); | |
Test(() => TestForNotEqual(list), "List<T> for i != Count"); | |
Test(() => TestForNotEqual(ilist), "IList<T> for i != Count"); | |
Test(() => TestForNotEqualPrefix(array), "Array for i != Length; ++i"); | |
Test(() => TestForNotEqualPrefix(list), "List<T> for i != Count; ++i"); | |
Test(() => TestForNotEqualPrefix(ilist), "IList<T> for i != Count; ++i"); | |
Test(() => TestForCachedLength(array), "Array for, cached length"); | |
Test(() => TestForCachedLength(list), "List<T> for, cached length"); | |
Test(() => TestForCachedLength(ilist), "IList<T> for, cached length"); | |
Test(() => TestForCachedLengthPrefix(array), "Array for, cached length, ++i"); | |
Test(() => TestForCachedLengthPrefix(list), "List<T> for, cached length, ++i"); | |
Test(() => TestForCachedLengthPrefix(ilist), "IList<T> for, cached length, ++i"); | |
Test(() => TestForCachedLengthNotEqual(array), "Array for, cached length, i != Length"); | |
Test(() => TestForCachedLengthNotEqual(list), "List<T> for, cached length, i != Count"); | |
Test(() => TestForCachedLengthNotEqual(ilist), "IList<T> for, cached length, i != Count"); | |
Test(() => TestForCachedLengthNotEqualPrefix(array), "Array for, cached length, i != Length; ++i"); | |
Test(() => TestForCachedLengthNotEqualPrefix(list), "List<T> for, cached length, i != Count; ++i"); | |
Test(() => TestForCachedLengthNotEqualPrefix(ilist), "IList<T> for, cached length, i != Count; ++i"); | |
Test(() => TestForeach(array), "Array foreach"); | |
Test(() => TestForeach(list), "List<T> foreach"); | |
Test(() => TestForeach(ilist), "IList<T> foreach"); | |
Test(() => TestForeach(ienumerable), "IEnumerable<T> foreach"); | |
Test(() => TestArrayForEach(array), "Array.ForEach"); | |
Test(() => TestListForEach(list), "List<T>.ForEach"); | |
} | |
Console.WriteLine("========"); | |
var rr = results.Select(r => | |
new | |
{ | |
Name = r.Key, | |
Average = r.Value.Average(), | |
Min = r.Value.Min(), | |
Max = r.Value.Max() | |
}).OrderBy(x => x.Average); | |
foreach (var result in rr) | |
{ | |
Console.WriteLine(result.Name + ": " + result.Average + ", Min: " + result.Min + ", Max: " + result.Max); | |
} | |
Console.WriteLine("========"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment