Skip to content

Instantly share code, notes, and snippets.

@atheken
Created February 14, 2010 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atheken/304184 to your computer and use it in GitHub Desktop.
Save atheken/304184 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class MyClass
{
public static void RunSnippet()
{
DateTime start = DateTime.Now;
for(int i = 0; i< 1000000; i++)
{
MyClass p = new MyClass();
}
Console.WriteLine("1000000 instantiated in {0}ms", (DateTime.Now-start).TotalMilliseconds);
start = DateTime.Now;
for(int i = 0; i< 1000000; i++)
{
MyClass p = Activator.CreateInstance<MyClass>();
}
Console.WriteLine("1000000 instantiated in {0}ms using generic method.", (DateTime.Now-start).TotalMilliseconds);
start = DateTime.Now;
for(int i = 0; i< 1000000; i++)
{
Object p = Activator.CreateInstance(typeof(MyClass));
}
Console.WriteLine("Activator 1000000 instantiated in {0}ms", (DateTime.Now-start).TotalMilliseconds);
start = DateTime.Now;
for(int i = 0; i< 1000000; i++)
{
MyClass p = (MyClass)Activator.CreateInstance(typeof(MyClass));
}
Console.WriteLine("Activator 1000000 instantiated in {0}ms then casted", (DateTime.Now-start).TotalMilliseconds);
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment