Generics and Object Casting– Boxing , UnboxingPosted: 2011/03/20
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.
Now lets perform a simple test by creating instances of both classes and performing operation. Stopwatch will be used to check performance.
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.
Second class uses default Interface.
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.
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.