C# performance – class vs struct

I do two tests to check if we can improve performance of application when we use struct instead of class for small objects.

First example:

using System;
using System.Diagnostics;

namespace PerformanceTestCSharp
{
    class PointC
    {
        public PointC(double x, double y)
        {
            this._x = x;
            this._y = y;
        }

        private double _x;
        public double X
        {
            get { return this._x; }
            set { this._x = value; }
        }

        private double _y;
        public double Y
        {
            get { return this._y; }
            set { this._y = value; }
        }
    }

    struct PointS
    {
        public PointS(double x, double y)
        {
            this._x = x;
            this._y = y;
        }

        private double _x;
        public double X
        {
            get { return this._x; }
            set { this._x = value; }
        }

        private double _y;
        public double Y
        {
            get { return this._y; }
            set { this._y = value; }
        }
    }

    class Program
    {
        const int Count = 10000000;

        static void Main()
        {
            TestStruct();
            TestClass();
        }

        public static void TestStruct()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            PointS[] points = new PointS[Count];

            for (int i = 0; i < Count; i++)
                points[i] = new PointS(i + 0.1, i + 1.1);

            stopwatch.Stop();
            Console.WriteLine("Struct: {0}", stopwatch.ElapsedMilliseconds);
            GC.Collect();
        }

        public static void TestClass()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            PointC[] points = new PointC[Count];

            for (int i = 0; i < Count; i++)
                points[i] = new PointC(i + 0.1, i + 1.1);

            stopwatch.Stop();
            Console.WriteLine("Class: {0}", stopwatch.ElapsedMilliseconds);
            GC.Collect();
        }
    }
}

And output:

Struct: 226
Class: 2460

And the second test (I add interface):

using System;
using System.Diagnostics;

namespace PerformanceTestCSharp
{
    public interface IPoint
    {
        double X { get; set; }
        double Y { get; set; }
    }

    class PointC : IPoint
    {
        public PointC(double x, double y)
        {
            this._x = x;
            this._y = y;
        }

        private double _x;
        public double X
        {
            get { return this._x; }
            set { this._x = value; }
        }

        private double _y;
        public double Y
        {
            get { return this._y; }
            set { this._y = value; }
        }
    }

    struct PointS : IPoint
    {
        public PointS(double x, double y)
        {
            this._x = x;
            this._y = y;
        }

        private double _x;
        public double X
        {
            get { return this._x; }
            set { this._x = value; }
        }

        private double _y;
        public double Y
        {
            get { return this._y; }
            set { this._y = value; }
        }
    }

    class Program
    {
        const int Count = 10000000;

        static void Main()
        {
            TestStruct();
            TestClass();
        }

        public static void TestStruct()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            IPoint[] points = new IPoint[Count];

            for (int i = 0; i < Count; i++)
                points[i] = new PointS(i + 0.1, i + 1.1);

            stopwatch.Stop();
            Console.WriteLine("Struct: {0}", stopwatch.ElapsedMilliseconds);
            GC.Collect();
        }

        public static void TestClass()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            IPoint[] points = new IPoint[Count];

            for (int i = 0; i < Count; i++)
                points[i] = new PointC(i + 0.1, i + 1.1);

            stopwatch.Stop();
            Console.WriteLine("Class: {0}", stopwatch.ElapsedMilliseconds);
            GC.Collect();
        }
    }
}

And output:

Struct: 2679
Class: 2606

You can see the difference! If we know what we do we can improve a performance.
But any situation as adding an interface to a struct can be slower than an our earlier implementation!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s