Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe Servy was correct it is a problem with your test. I reversed the order of the tests (just a hunch):</p> <pre><code>internal class Program { public interface Base { Int64 Size { get; } } public class Derived : Base { public Int64 Size { get { return 10; } } } public class GenericProcessor&lt;TT&gt; where TT : Base { private Int64 sum; public GenericProcessor() { sum = 0; } public void process(TT o) { sum += o.Size; } public Int64 Sum { get { return sum; } } } public class PolymorphicProcessor { private Int64 sum; public PolymorphicProcessor() { sum = 0; } public void process(Base o) { sum += o.Size; } public Int64 Sum { get { return sum; } } } private static void Main(string[] args) { var generic_processor = new GenericProcessor&lt;Derived&gt;(); var polymorphic_processor = new PolymorphicProcessor(); Stopwatch sw = new Stopwatch(); int N = 100000000; var derived = new Derived(); sw.Start(); for (int i = 0; i &lt; N; ++i) polymorphic_processor.process(derived); sw.Stop(); Console.WriteLine( "Sum =" + polymorphic_processor.Sum + " Poly performance = " + sw.ElapsedMilliseconds + " millisec"); sw.Restart(); sw.Start(); for (int i = 0; i &lt; N; ++i) generic_processor.process(derived); sw.Stop(); Console.WriteLine( "Sum =" + generic_processor.Sum + " Generic performance = " + sw.ElapsedMilliseconds + " millisec"); Console.Read(); } } </code></pre> <p>In this case the polymorphic is slower in my tests. This shows that the first test is significantly slower than the second test. It could be loading classes the first time, preemptions, who knows ...</p> <p>I just want to note that I am not arguing that generics are faster or as fast. I'm simply trying to prove that these kinds of tests don't make a case one way or the other.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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