This program shows the difference between Generics and Non-Generic . It checks the performance of Generics. Generics are faster than Non-Generics it takes less time to add and retrieve any type of values. Below program gives the demonstration on performance and shows how powerful is Generics.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace GenericsPerformance
{
class Program
{
static void Main(string[] args)
{
NonGenericMethod();
GenericMethods();
}
static void NonGenericMethod()
{
long OperarionTime = 0;
ArrayList list = new ArrayList();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 1; i <= 10000000; i++)
{
list.Add(i);
}
OperarionTime = sw.ElapsedMilliseconds;
Console.WriteLine("arrayList {0} values add time is {1} milli",list.Count,OperarionTime);
sw.Restart();
foreach (int i in list)
{
int value = i;
}
OperarionTime = sw.ElapsedMilliseconds;
Console.WriteLine("array list {0} values retrive time is {1} milli",list.Count,OperarionTime);
Console.WriteLine("-------------------------------------------------------------------");
}
static void GenericMethods()
{
long OperarionTime = 0;
List<int> list = new List<int>();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 1; i <= 10000000; i++)
{
list.Add(i);
}
OperarionTime = sw.ElapsedMilliseconds;
Console.WriteLine("list<> {0} values add time is {1} milli",list.Count,OperarionTime);
sw.Restart();
foreach (int i in list)
{
int value = i;
}
OperarionTime = sw.ElapsedMilliseconds;
Console.WriteLine("list<> {0} values retreve time is {1} milli", list.Count, OperarionTime);
}
}
}
Note:- Here ArrayList is Non-generic collection class and List<int> is Generic class.
Thats an interesting post tushar
ReplyDeleteThats an interesting post tushar
ReplyDelete