Perfomance thoughts on interception Post 2 of several

Ok now lets try to measure another thing. What is the cost of class instantiation:

So my test code is something like this:

    public static void Main()
    {
        long costs=0;
        int times = 10;
        for (int i = 0; i < 10; i++)
        {
            costs += test2();
        }
        Console.WriteLine($"Average is {costs/times}");
        Console.ReadLine();
    }
    public class Class1
    {
        private string param1;
        private string param2;
        private int x;

        public Class1() { }
        public Class1(string param1, string param2, int x)
        {
            this.param1 = param1;
            this.param2 = param2;
            this.x = x;
        }
    }
    static long test2()
    {
        var st = new Stopwatch();
        Class1 holder;
        st.Start();
        for (var j = 0; j < 10000000; j++)
        {

        }
        st.Stop();
        var setupcost = st.ElapsedMilliseconds;
        Console.WriteLine("Taking just the loop cost ms " + setupcost);
        st.Reset();
        st.Start();
        for (var j = 0; j < 10000000; j++)
        {
            holder = new Class1();
        }
        st.Stop();
        var cost = st.ElapsedMilliseconds;
        Console.WriteLine("new with parameterless constructor ms " + (cost - setupcost));
        return cost;
    }

The measurement indicates an average cost of: 148ms

Changing the code to instantiate the class with something like: holder = new Class1("aaaaa","bbbbbb",10);

Gives an average cost of: 198ms
Changing Class1 to be an struct changes the average cost to: 134ms

Now on release the numbers are:
parameterless class constructor: 60
class constructor: 96
struct constructor: 56

However not always is better to have a struct because they can be involved in some autoboxing processes. Lets add an interface and change the code to:

    public static void Main()
    {
        long costs=0;
        int times = 10;
        for (int i = 0; i < 10; i++)
        {
            costs += test2();
        }
        Console.WriteLine($"Average is {costs/times}");
        Console.ReadLine();
    }

    public interface MyInterface
    {
        int Foo();
    }
    public class Class1 : MyInterface
    {

        private string param1;
        private string param2;
        private int x;

        public Class1(string param1, string param2, int x)
        {
            this.param1 = param1;
            this.param2 = param2;
            this.x = x;
        }

        public int Foo()
        {
            throw new NotImplementedException();
        }
    }
    static long test2()
    {
        var st = new Stopwatch();
        Class1 holder;
        MyInterface holder2;
        st.Start();
        for (var j = 0; j < 10000000; j++)
        {
            holder2 = null;
        }
        st.Stop();
        var setupcost = st.ElapsedMilliseconds;
        Console.WriteLine("Taking just the loop cost ms " + setupcost);
        st.Reset();
        st.Start();
        for (var j = 0; j < 10000000; j++)
        {
            holder2 = new Class1("aaaaa","bbbbbb",10);
        }
        st.Stop();
        var cost = st.ElapsedMilliseconds;
        Console.WriteLine("Cost of class instantiation with parameterless constructor ms " + (cost - setupcost));
        return cost;
    }

The average time for the class case is still the same: 197ms
The average tome fo rthe struct case is different: 229ms

However my numbers on release are different:

The average time for the class case is still the same: 95ms
The average tome fo rthe struct case is different: 56ms