Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've done a test, my results for 1000000 iterations are approximately</p> <pre><code>ByCast 166ms ByType 84ms ByEnum 98ms </code></pre> <p>So the <code>enum</code> is in fact superfluous and slower but, not by much. This should not be too suprising, the type system is fundamental to the .Net Framework.</p> <p>Test code transcribed below, apologies for errata</p> <pre><code>using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; class Program { private enum TypeOfFact { Gdp, Other } private abstract class Fact { public virtual int Year { get; set; } public abstract TypeOfFact FactType { get; } } private class GdpFact : Fact { public override TypeOfFact FactType { get { return TypeOfFact.Gdp; } } } private class OtherFact : Fact { public override TypeOfFact FactType { get { return TypeOfFact.Other; } } } static void Main() { Ilist&lt;Fact&gt; facts = new List&lt;Fact&gt; { new GdpFact { Year = 2010 }, new OtherFact { Year = 2010 }, new GdpFact { Year = 2009 }, new OtherFact { Year = 2009 }, new GdpFact { Year = 2011 }, new OtherFact { Year = 2011 }, }; const int interations = 1000000; var funcs = new List&lt;Func&lt;IList&lt;Fact&gt;, Fact&gt;&gt; { ByList, ByType, ByEnum }; // Warmup foreach (var func in funcs) { Measure(5, func, facts); } // Results foreach (var result in funcs.Select(f =&gt; new { Description = f.Method.Name, Ms = Measure(iterations, f, facts) })) { Console.WriteLine( "{0} time = {1}ms", result.Description, result.Ms); } } private static long Measure( int iterations, Func&lt;IList&lt;Fact&gt;, Fact&gt; func, IList&lt;Fact&gt; facts) { var stopwatch = new Stopwatch(); stopwatch.Start(); for (var i = 0; i &lt; iterations; i++) { func.Invoke(facts); } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; } private static Fact ByType(IList&lt;Fact&gt; facts) { return facts.FirstOrDefault(f =&gt; f.Year == 2011 &amp;&amp; f is GdpFact); } private static Fact ByEnum(IList&lt;Fact&gt; facts) { return facts.FirstOrDefault(f =&gt; f.Year == 2011 &amp;&amp; f.FactType == TypeOfFact.Gdp); } private static Fact ByCast(IList&lt;Fact&gt; facts) { return facts.OfType&lt;GdpFact&gt;() .FirstOrDefault(f =&gt; f.Year == 2011); } } </code></pre> <hr> <p>This <a href="https://stackoverflow.com/questions/686412/c-sharp-is-operator-performance">question</a> seems relevant.</p>
 

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