Created
February 14, 2010 18:59
-
-
Save atheken/304184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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