C# 4.0 Dynamics vs. Reflection
Hi!
Ah ha, today I already wrote two more blog now I am moving on third one, I found new feature in C# 4.0 is Dynamics its to cool. its much faster than reflection mechanism also cached objects,
Simply I show demo given below,
First I create a Console Application then create a class name is myTestingClass
Snippet
Now I am going to call the above class by reflection and Dynamics.
Snippet
Code result shown in below daigram,
above example shown results, first took then second time gave the result in litle time than the first time.
Ah ha, today I already wrote two more blog now I am moving on third one, I found new feature in C# 4.0 is Dynamics its to cool. its much faster than reflection mechanism also cached objects,
Simply I show demo given below,
First I create a Console Application then create a class name is myTestingClass
public class myTestingClass
{
public string myProperty
{
get;
set;
}
}
Now I am going to call the above class by reflection and Dynamics.
static void Main(string[] args)
{
Stopwatch mystopWatch = new Stopwatch();
long rang = 1000000;
var myTestType = new myTestingClass();
var property = typeof(myTestingClass).GetProperty("myProperty");
mystopWatch.Start();
for (int i = 0; i < rang; i++)
{
property.SetValue(myTestType, "Hello World", null);
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " First Time Result");
dynamic myTestDynamic = new myTestingClass();
mystopWatch.Reset();
mystopWatch.Start();
for (int i = 0; i < rang; i++)
{
myTestDynamic.myProperty = "Hello World";
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " First Time Result");
// Console.ReadLine();
mystopWatch.Reset();
mystopWatch.Start();
for (int i = 0; i < rang; i++)
{
property.SetValue(myTestType, "Hello World", null);
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " Second Time Result");
myTestDynamic = new myTestingClass();
mystopWatch.Reset();
mystopWatch.Start();
for (int i = 0; i < rang; i++)
{
myTestDynamic.myProperty = "Hello World";
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " Second Time Result");
Console.ReadLine();
}
Code result shown in below daigram,
above example shown results, first took then second time gave the result in litle time than the first time.
Good one
ReplyDelete