Performance of 'is' vs typeof (Reflection) in C#
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < 100000000; i++)
{
CNDSegment segment = new CNDSegment();
if (segment is CNDSegment == false)
{
}
}
s.Stop();
Console.WriteLine(String.Format("Time taken by is {0} milliseconds", s.ElapsedMilliseconds));
s.Reset();
s.Start();
for (int i = 0; i < 100000000; i++)
{
CNDSegment segment = new CNDSegment();
if (segment.GetType() != typeof(CNDSegment))
{
}
}
s.Stop();
Console.WriteLine(String.Format("Time taken by GetType and typeof method {0} milliseconds", s.ElapsedMilliseconds));
Performance results for 100000000 comparisions
Time taken by is 1338 milliseconds
Time taken by GetType and typeof method 5050 milliseconds
s.Start();
for (int i = 0; i < 100000000; i++)
{
CNDSegment segment = new CNDSegment();
if (segment is CNDSegment == false)
{
}
}
s.Stop();
Console.WriteLine(String.Format("Time taken by is {0} milliseconds", s.ElapsedMilliseconds));
s.Reset();
s.Start();
for (int i = 0; i < 100000000; i++)
{
CNDSegment segment = new CNDSegment();
if (segment.GetType() != typeof(CNDSegment))
{
}
}
s.Stop();
Console.WriteLine(String.Format("Time taken by GetType and typeof method {0} milliseconds", s.ElapsedMilliseconds));
Performance results for 100000000 comparisions
Time taken by is 1338 milliseconds
Time taken by GetType and typeof method 5050 milliseconds
Comments
Post a Comment