RSS Feed

Generics and Object Casting– Boxing , Unboxing

There are multiple scenarios in which we need to create generic classes for different objects. In this case we have two options. Either we can create generic class and specify the type/types of the object in the code  or ,  we can create a class operating on the System.Object types which are the base for every object / class in the framework.

If we have two options available then  the question is which one is better ?

To test both approaches , I created a simple logic which performs assignment and some kind of operation which returns value. This logic is encapsulated in two classes. ClassGeneric is built with generics built in .Net .You can see that type is represented by “U” letter. ClassObject is based on casting to System.Object.

 

Code:

    class ClassGeneric<U>
    {
        U test;

        public ClassGeneric(U value)
        {
            test = value;
        }

        public void Operation()
        {
            U t = test;
        }
    }

    class ClassObject
    {
        Object test;

        public ClassObject(Object value)
        {
            test = value;
        }

        public void Operation()
        {
            int t = (int)test;
        }
    }

 

Test :

Now lets perform a simple test by creating instances of both classes and performing operation. Stopwatch will be used to check performance.

 

        static Stopwatch sw = new Stopwatch();

        static void Main(string[] args)
        {
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                new Labs.ClassGeneric<int>(1).Operation();
            }
            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                new Labs.ClassObject((object)1).Operation();
            }
            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds);

            Console.ReadLine();
        }

 

Result :

Generics 471k ticks  -  Objects  710k ticks

Generics 212 ms    Objects 343 ms

Why there is a difference ?

Generics are defined on the runtime.  .Net Framework based on the specified type in the code for example  (Queue<int>) creates a class with the type and stores reference to it. This operation is performed once on the start by the JIT-er (Just in time compiler). This operation is performed once so there is a minimal performance loss.

In case of System.Object class when casting from and to int we are performing Boxing and Unboxing operation. 

Boxing is performed every time we are casting Value Type to the reference Type. Boxing operation wraps our Value Type in a class deriving from the System.Object. This operation requires some cpu work. Same thing applies for the Unboxing operation which is performed when casting from Reference Type to the Value Type.

In this example I am casting int to Object type 1000000 times. This is the cause of the difference in time / performance. Generic classes does not require additional operations.

Boxing , Unboxing and generic interfaces

Understaing when your code perfmors boxing and unboxing is  really important. Check this example. In .Net we can implement various interfaces which are used in the Framework. Some of them are generic. We can define which type will be used.

Here I am implementing IComparable interface , which is usefull when you want to perform Sort operation on the Collection containing your custom Class.

First Class uses the Generic Interface.

    class ClassSort : IComparable<ClassSort>
    {
        public int A {get;set;}

       
        #region IComparable<int> Members

        public int CompareTo(ClassSort other)
        {
          throw new NotImplementedException();
        }

        #endregion
    }

Second class uses default Interface.

    class ClassSortOne : IComparable
    {
        public int A {get;set;}

        #region IComparable Members

        public int CompareTo(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

As you can see IComparable without generic type forces boxing when comparing objects beacuase we have to cast the objects. CompareTo() method in example with default interface uses object as a parameter while generic interface implements method with specified class as a parameter. Interface with specified generic type doesn’t need boxing and it is faster.

 

Conclusion:

Use generic classes as often as you can. Especially when making a lot of operations with them. Simple casting which causes boxing and unboxing process consumes a lot of processor time.

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