Skip to content

Instantly share code, notes, and snippets.

@hoganlong
Last active December 11, 2015 00:59
Show Gist options
  • Save hoganlong/4520437 to your computer and use it in GitHub Desktop.
Save hoganlong/4520437 to your computer and use it in GitHub Desktop.
Testing some different ways to check type vs property. IS wins. (yay?)
class TestMe
{
public string test = "test";
}
void Main()
{
List<TestMe> aList = new List<TestMe>();
for (int index = 0; index < 10000000;index++)
{
aList.Add(new TestMe());
}
var timer1 = new System.Diagnostics.Stopwatch();
timer1.Start();
if (aList.OfType<TestMe>().ToList().Count < aList.Count)
throw new Exception("Panic!");
timer1.Stop();
Console.WriteLine("OfType time: {0}", timer1.Elapsed);
timer1.Reset();
timer1.Start();
Type TestMeType = new TestMe().GetType();
foreach (TestMe t in aList)
{
if (aList.GetType() == TestMeType)
throw new Exception("Panic!");
}
timer1.Stop();
Console.WriteLine("GetType time: {0}", timer1.Elapsed);
timer1.Reset();
timer1.Start();
foreach (TestMe t in aList)
{
if (!(t.test == "test"))
throw new Exception("Panic!");
}
timer1.Stop();
Console.WriteLine("property time: {0}", timer1.Elapsed);
timer1.Reset();
timer1.Start();
foreach (TestMe t in aList)
{
if (!(t is TestMe))
throw new Exception("Panic!");
}
timer1.Stop();
Console.WriteLine("is time: {0}", timer1.Elapsed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment