Assembly Neutral Interface Performance

I already wrote how impressed I was by the new Assembly Neutral Interface feature in aspnet vNext. So I decided to continue my experiment and test the performance.

In order to have something to compare to, I wrote an identical interface to the one I wrote for the previous post.

public interface ITestType
{
    string Get();
}

public class TestType : ITestType
{
    public string Get()
    {
        return "Got";
    }
}

I then wrote this test code, and ran it a few times in release mode.

public void Main(string[] args)
{
    var myTypeTime = TimedExecution(() => { IMyType t = new MyType(); } , 1);
    var testTypeTime = TimedExecution(() => { ITestType t = new TestType(); }, 1);

    Console.WriteLine("Assembly Neutral Interface first create time: " + myTypeTime.Ticks + "ticks");
    Console.WriteLine("Normal Interface first create time: " + testTypeTime.Ticks + "ticks");

    IMyType myType = new MyType();
    ITestType testType = new TestType();
    
    myTypeTime = TimedExecution(() => myType.Get(), 1);
    testTypeTime = TimedExecution(() => testType.Get(), 1);

    Console.WriteLine("Assembly Neutral Interface first execution time: " + myTypeTime.Ticks + "ticks");
    Console.WriteLine("Normal Interface execution first time: " + testTypeTime.Ticks + "ticks");

    myTypeTime = TimedExecution(() => myType.Get(), 10000000);
    testTypeTime = TimedExecution(() => testType.Get(), 10000000);

    Console.WriteLine("Assembly Neutral Interface execution time after warmup: " + myTypeTime.TotalMilliseconds + "ms");
    Console.WriteLine("Normal Interface execution time after warmup: " + testTypeTime.TotalMilliseconds + "ms");

    myTypeTime = TimedExecution(() =>
    {
        IMyType m = new MyType();
        m.Get();
    }, 10000000);

    testTypeTime = TimedExecution(() =>
    {
        ITestType t = new TestType();
        t.Get();
    }, 10000000);

    Console.WriteLine("Assembly Neutral Interface creation and execution time: " + myTypeTime.TotalMilliseconds + "ms");
    Console.WriteLine("Normal Interface creation and execution time: " + testTypeTime.TotalMilliseconds + "ms");
    
    Console.ReadLine();
}

The results show that the performance is a bit more expensive for instantiation but practically identical for execution. However it seems that there is a small warmup cost.

Assembly_Neutral_Interface_PerfTest_1

Assembly_Neutral_Interface_PerfTest_2

Assembly_Neutral_Interface_PerfTest_3

Leave a Reply