Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Count() Extension Method Performance
    primarykey
    data
    text
    <p>If the LINQ <code>Count()</code> extension method is invoked on an <code>IEnumerable&lt;T&gt;</code> that has a <code>Count</code> property (e.g. <code>List&lt;T&gt;</code>), does the <code>Count()</code> method look for that property and return it (rather than counting the items by enumerating them)? The following test code seems to indicate that it does:</p> <pre><code>using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace CountSpeedTest { // Output: // List - CLR : 0 ms // Enumerate - CLR : 10 ms // List - Mine: 12 ms // Enumerate - Mine: 12 ms class Program { private const int Runs = 10; private const int Items = 1000000; static void Main(string[] args) { var total = new long[] {0, 0, 0, 0}; for (int i = 0; i &lt; Runs; ++i) { var items = Enumerable.Range(0, Items).Select(o =&gt; o.ToString()).ToList(); var list = new List&lt;string&gt;(items); var enumerate = new Enumerate&lt;string&gt;(items); total[0] += TimeCount(list, c =&gt; c.Count()); total[1] += TimeCount(enumerate, c =&gt; c.Count()); total[2] += TimeCount(list, c =&gt; c.SlowCount()); total[3] += TimeCount(enumerate, c =&gt; c.SlowCount()); } Console.WriteLine(String.Format("List - CLR : {0} ms", total[0] / Runs)); Console.WriteLine(String.Format("Enumerate - CLR : {0} ms", total[1] / Runs)); Console.WriteLine(String.Format("List - Mine: {0} ms", total[2] / Runs)); Console.WriteLine(String.Format("Enumerate - Mine: {0} ms", total[3] / Runs)); Console.ReadKey(true); } private static long TimeCount&lt;T&gt;(IEnumerable&lt;T&gt; collection, Func&lt;IEnumerable&lt;T&gt;, int&gt; counter) { var stopwatch = Stopwatch.StartNew(); var count = counter(collection); stopwatch.Stop(); if (count != Items) throw new Exception("Incorrect Count"); return stopwatch.ElapsedMilliseconds; } } public static class CountExtensions { // Performs a simple enumeration based count. public static int SlowCount&lt;T&gt;(this IEnumerable&lt;T&gt; items) { var i = 0; var enumerator = items.GetEnumerator(); while (enumerator.MoveNext()) i++; return i; } } // Wraps an IEnumerable&lt;T&gt; to hide its Count property. public class Enumerate&lt;T&gt; : IEnumerable&lt;T&gt; { private readonly IEnumerable&lt;T&gt; collection; public Enumerate(IEnumerable&lt;T&gt; collection) { this.collection = collection; } public IEnumerator&lt;T&gt; GetEnumerator() { return collection.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } } </code></pre> <p><strong>On a related note:</strong> how can a custom collection which implements <code>IEnumerable&lt;T&gt;</code> expose its own <code>Count</code> property in such a way that the CLR <code>Count()</code> extension method can take advantage of it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload