Last active
July 26, 2016 20:58
-
-
Save kspearrin/8dbf7d76d6fd21a8d861dc9c472547d4 to your computer and use it in GitHub Desktop.
CAREFUL! IEnumerable can be inefficient and slow.
This file contains 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
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
public class Program | |
{ | |
// CAREFUL! IEnumerable can be inefficient and slow. | |
public static void Main() | |
{ | |
var size = 10000; | |
var numbers = new List<int>(); | |
for (int i = 0; i < size; i++) | |
{ | |
numbers.Add(i); | |
} | |
// Make the three collections | |
var enumNumbers = numbers.Where(n => true); | |
var listNumbers = enumNumbers.ToList() as IEnumerable<int>; | |
var arrayNumbers = enumNumbers.ToArray() as IEnumerable<int>; | |
Console.WriteLine("==== ElementAt() ===="); | |
var sw = Stopwatch.StartNew(); | |
// IEnumerable | |
for (int i = 0; i < size; i++) | |
{ | |
enumNumbers.ElementAt(i); | |
} | |
Console.WriteLine("Enumerable: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
// List | |
for (int i = 0; i < size; i++) | |
{ | |
listNumbers.ElementAt(i); | |
} | |
Console.WriteLine("List: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
// Array | |
for (int i = 0; i < size; i++) | |
{ | |
arrayNumbers.ElementAt(i); | |
} | |
Console.WriteLine("Array: " + sw.ElapsedMilliseconds + " ms"); | |
Console.WriteLine(); | |
Console.WriteLine("==== Count() ===="); | |
sw.Restart(); | |
// IEnumerable | |
for (int i = 0; i < size; i++) | |
{ | |
enumNumbers.Count(); | |
} | |
Console.WriteLine("Enumerable: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
// List | |
for (int i = 0; i < size; i++) | |
{ | |
listNumbers.Count(); | |
} | |
Console.WriteLine("List: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Restart(); | |
// Array | |
for (int i = 0; i < size; i++) | |
{ | |
arrayNumbers.Count(); | |
} | |
Console.WriteLine("Array: " + sw.ElapsedMilliseconds + " ms"); | |
sw.Stop(); | |
} | |
} | |
/* | |
Results: | |
==== ElementAt() ==== | |
Enumerable: 1244 ms | |
List: 0 ms | |
Array: 0 ms | |
==== Count() ==== | |
Enumerable: 2993 ms | |
List: 0 ms | |
Array: 0 ms | |
Ref. source from IEnumerable<T>: | |
public static int Count<TSource>(this IEnumerable<TSource> source) | |
{ | |
if (source == null) | |
throw Error.ArgumentNull("source"); | |
ICollection<TSource> collectionoft = source as ICollection<TSource>; | |
if (collectionoft != null) | |
return collectionoft.Count; | |
ICollection collection = source as ICollection; | |
if (collection != null) | |
return collection.Count; | |
int count = 0; | |
using (IEnumerator<TSource> e = source.GetEnumerator()) | |
{ | |
checked | |
{ | |
while (e.MoveNext()) | |
count++; | |
} | |
} | |
return count; | |
} | |
public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index) | |
{ | |
if (source == null) | |
throw Error.ArgumentNull("source"); | |
IList<TSource> list = source as IList<TSource>; | |
if (list != null) | |
return list[index]; | |
if (index < 0) | |
throw Error.ArgumentOutOfRange("index"); | |
using (IEnumerator<TSource> e = source.GetEnumerator()) | |
{ | |
while (true) | |
{ | |
if (!e.MoveNext()) | |
throw Error.ArgumentOutOfRange("index"); | |
if (index == 0) | |
return e.Current; | |
index--; | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment