Performance difference between Class and Structure

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 1885 red flag

Performance difference between Class and Structure

Performance difference between Class and Structure

In this article we will try to understand the performance difference between class and structure. If you are experience developer or have C++ background then probably you know both concepts. The class is reference type where structure is value type.

Generally in time of application development we use class rather than using structure. If we compare the execution speed of both then we will see that structure is much faster than class. So, to improve the performance of C# application we can use structure rather than class, if possible. Try to understand below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApp
{
    class TestClass
    {
        public string Name { get; set; }
    }
 
    struct TestStruct
    {
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestClass c = null;
            TestStruct s;
 
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for(int i=0; i<100 ;i++)
            {
                c = new TestClass();
                c.Name = "Sourav";
           }
            sw.Stop();
            Console.WriteLine("Using Class:- " + sw.ElapsedTicks);
 
            sw.Restart();
            for (int i = 0; i < 100; i++)
            {
                s = new TestStruct();
                s.Name = "Sourav";
            }
            sw.Stop();
           
            Console.WriteLine("Using Strecture:- " + sw.ElapsedTicks);
            
            Console.ReadLine();
        }
    }
}
 

In this above example we have defined both class and structure to measure the performance between them.  Within for loop we are creating huge amount of object and using StopWatch class we are counting tick (millisecond).

Here is sample output.


The output is taken with the configuration of core i3 processor and 4GB of internal memory.  If you test this code in different hardware configuration then the output might change but the output type will very similar with this sample output.

In our output, we are seeing that structure is taking nearly half time then class for same operation.

Now, the design is why class is taking much time than structure. As we know that class is reference type and when we create object of class the object get create in heap memory area and one reference get create which get place on the top of stack. And the reference which creates on top of stack points the object which has created in heap memory location. To create and adjust all the pointer it takes little time.

In case of structure, the object get create on top of stack and there is no adjustment of reference and pointer, so generally it executes faster than class.

When to use class and when structure?

This question is very important to discuss. When we will choose class and when structure. Generally if the data is very complex, if there is need to implement lot of complex function then use class blindly. If the data type is very light weight and contains simple member and function then we can use structure.

 Conclusion:-

In this quick article we have learn the performance difference between class and structure.  It is always recommended to use structure when possible. Hope you have understood the concept.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)