Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics vs Object performance

Tags:

c#

I'm doing practice problems from MCTS Exam 70-536 Microsft .Net Framework Application Dev Foundation, and one of the problems is to create two classes, one generic, one object type that both perform the same thing; in which a loop uses the class and iterated over thousand times. And using the timer, time the performance of both. There was another post at C# generics question that seeks the same questoion but nonone replied.

Basically if in my code I run the generic class first it takes loger to process. If I run the object class first than the object class takes longer to process. The whole idea was to prove that generics perform faster.

I used the original users code to save me some time. I didn't particularly see anything wrong with the code and was puzzled by the outcome. Can some one explain why the unusual results?

Thanks,

Risho

Here is the code:

class Program
{
    class Object_Sample
    {            
        public Object_Sample()
        {
            Console.WriteLine("Object_Sample Class");
        }

        public long getTicks()
        {
            return DateTime.Now.Ticks;
        }

        public void display(Object a)
        {
            Console.WriteLine("{0}", a);
        }
    }

    class Generics_Samle<T>
    {            
        public Generics_Samle()
        {
            Console.WriteLine("Generics_Sample Class");
        }

        public long getTicks()
        {
            return DateTime.Now.Ticks;
        }

        public void display(T a)
        {
            Console.WriteLine("{0}", a);
        }
    }

    static void Main(string[] args)
    {            
        long ticks_initial, ticks_final, diff_generics, diff_object;
        Object_Sample OS = new Object_Sample();
        Generics_Samle<int> GS = new Generics_Samle<int>();

        //Generic Sample
        ticks_initial = 0;
        ticks_final = 0;
        ticks_initial = GS.getTicks();

        for (int i = 0; i < 50000; i++)
        {
            GS.display(i);
        }
        ticks_final = GS.getTicks();
        diff_generics = ticks_final - ticks_initial;

        //Object Sample
        ticks_initial = 0;
        ticks_final = 0;
        ticks_initial = OS.getTicks();

        for (int j = 0; j < 50000; j++)
        {
            OS.display(j);
        }

        ticks_final = OS.getTicks();
        diff_object = ticks_final - ticks_initial;

        Console.WriteLine("\nPerformance of Generics {0}", diff_generics);
        Console.WriteLine("Performance of Object {0}", diff_object);

        Console.ReadKey();
    }
}
like image 485
Risho Avatar asked Jan 03 '11 14:01

Risho


People also ask

Do generics increase performance?

With using generics, your code will become reusable, type safe (and strongly-typed) and will have a better performance at run-time since, when used properly, there will be zero cost for type casting or boxing/unboxing which in result will not introduce type conversion errors at runtime.

Why do we use generics instead of object?

Generics could be used to develop a better solution using a container that can have a type assigned at instantiation, otherwise referred to as a generic type, allowing the creation of an object that can be used to store objects of the assigned type.

What is the biggest advantage of generics?

One of the big advantages of generics is performance. Using value types with non – generic collection classes results in boxing and unboxing when the value type is converted to a reference type and vice versa.

When would you use generics in your code?

Use generic types to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The . NET class library contains several generic collection classes in the System.


2 Answers

Well, the first problem I can see is that you're using the DateTime object to measure time in your application (for a very small interval).

You should be using the Stopwatch class. It offers better precision when trying to benchmark code.

The second problem is that you're not allowing for JIT (Just-In-Time compilation). The first call to your code is going to take longer simply because it has to be JIT'd. After that, you'll get your results.

I would make a single call in to your code before you start timing things so you can get an accurate idea of what is happening during the loop.

like image 150
Justin Niessner Avatar answered Sep 28 '22 11:09

Justin Niessner


You should run both classes a separate time before timing it to allow the JITter to run.

like image 28
SLaks Avatar answered Sep 28 '22 11:09

SLaks