‘as’ versus type casting
Prefix casting vs as casting
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < 100000000; i++)
{
object myClass = new CNDSegment();
((CNDSegment)myClass).Method1();
}
s.Stop();
Console.WriteLine(String.Format("Time taken by prefix casting is {0} milliseconds", s.ElapsedMilliseconds));
s.Reset();
s.Start();
for (int i = 0; i < 100000000; i++)
{
object myClass = new CNDSegment();
(myClass as CNDSegment).Method1();
}
s.Stop();
Console.WriteLine(String.Format("Time taken by as casting and typeof method {0} milliseconds", s.ElapsedMilliseconds));
Console.ReadLine();
Time taken by prefix casting is 1406 milliseconds
Time taken by as casting and typeof method 1338 milliseconds
Read this:
http://www.codeproject.com/Articles/8052/Type-casting-impact-over-execution-performance-in#_rating
http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < 100000000; i++)
{
object myClass = new CNDSegment();
((CNDSegment)myClass).Method1();
}
s.Stop();
Console.WriteLine(String.Format("Time taken by prefix casting is {0} milliseconds", s.ElapsedMilliseconds));
s.Reset();
s.Start();
for (int i = 0; i < 100000000; i++)
{
object myClass = new CNDSegment();
(myClass as CNDSegment).Method1();
}
s.Stop();
Console.WriteLine(String.Format("Time taken by as casting and typeof method {0} milliseconds", s.ElapsedMilliseconds));
Console.ReadLine();
Time taken by prefix casting is 1406 milliseconds
Time taken by as casting and typeof method 1338 milliseconds
Read this:
http://www.codeproject.com/Articles/8052/Type-casting-impact-over-execution-performance-in#_rating
http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/
Comments
Post a Comment