Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created June 27, 2014 23:34
Show Gist options
  • Save Buildstarted/e6646dfd8fcf000efd50 to your computer and use it in GitHub Desktop.
Save Buildstarted/e6646dfd8fcf000efd50 to your computer and use it in GitHub Desktop.
namespace CastClassVsIsInst
{
using System;
using System.Diagnostics;
class Program
{
private class Blah { }
static void Main(string[] args)
{
object instance = new Blah();
Measure(() => { var copy = (Blah)instance; }, 5);
Measure(() => { var copy = instance as Blah; }, 5);
Console.ReadKey();
}
private static void Measure(Action action, int times)
{
action();
for (var time = 0; time < times; time++)
{
var watch = Stopwatch.StartNew();
for (var iteration = 0; iteration < int.MaxValue; iteration++)
{
action();
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
GC.Collect();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment