Last active
August 29, 2015 14:02
-
-
Save hazzik/52bc80e2ba1b8f5503c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Lazy using generic | |
Lazy using generic finished in 12227 | |
Lazy using activator | |
Lazy using activator finished in 11902 | |
Lazy using generic activator | |
Lazy using generic activator finished in 14291 | |
Lazy using func | |
Lazy using func finished in 6625 |
This file contains hidden or 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.Diagnostics; | |
public class Class1 | |
{ | |
} | |
public class Test1 | |
{ | |
private const int iterations = 10000000; | |
public static void Main() | |
{ | |
Create1<Class1>(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCApproach(); | |
GC.WaitForFullGCComplete(); | |
Create2<Class1>(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCApproach(); | |
GC.WaitForFullGCComplete(); | |
Create3<Class1>(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCApproach(); | |
GC.WaitForFullGCComplete(); | |
Create4<Class1>(() => new Class1()); | |
} | |
public static void Create1<T>() where T : new() | |
{ | |
var watch = new Stopwatch(); | |
var lazies = new Lazy<T>[iterations]; | |
var instances = new T[iterations]; | |
Console.WriteLine("Lazy using generic"); | |
watch.Start(); | |
for (var i = 0; i < iterations; i++) | |
lazies[i] = Factory1<T>(); | |
for (var i = 0; i < iterations; i++) | |
instances[i] = lazies[i].Value; | |
watch.Stop(); | |
Console.WriteLine("Lazy using generic finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory1<T>() where T : new() | |
{ | |
return new Lazy<T>(() => new T()); | |
} | |
public static void Create2<T>() where T : new() | |
{ | |
var watch = new Stopwatch(); | |
var lazies = new Lazy<T>[iterations]; | |
var instances = new T[iterations]; | |
Console.WriteLine("Lazy using activator"); | |
watch.Reset(); | |
watch.Start(); | |
for (var i = 0; i < iterations; i++) | |
lazies[i] = Factory2<T>(); | |
for (var i = 0; i < iterations; i++) | |
instances[i] = lazies[i].Value; | |
watch.Stop(); | |
Console.WriteLine("Lazy using activator finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory2<T>() where T : new() | |
{ | |
return new Lazy<T>(() => (T)Activator.CreateInstance(typeof(T))); | |
} | |
public static void Create3<T>() where T : new() | |
{ | |
var watch = new Stopwatch(); | |
var lazies = new Lazy<T>[iterations]; | |
var instances = new T[iterations]; | |
Console.WriteLine("Lazy using generic activator"); | |
watch.Reset(); | |
watch.Start(); | |
for (var i = 0; i < iterations; i++) | |
lazies[i] = Factory3<T>(); | |
for (var i = 0; i < iterations; i++) | |
instances[i] = lazies[i].Value; | |
watch.Stop(); | |
Console.WriteLine("Lazy using generic activator finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory3<T>() where T : new() | |
{ | |
return new Lazy<T>(() => Activator.CreateInstance<T>()); | |
} | |
public static void Create4<T>(Func<T> func) | |
{ | |
var watch = new Stopwatch(); | |
var lazies = new Lazy<T>[iterations]; | |
var instances = new T[iterations]; | |
Console.WriteLine("Lazy using func"); | |
watch.Reset(); | |
watch.Start(); | |
for (var i = 0; i < iterations; i++) | |
lazies[i] = Factory4(func); | |
for (var i = 0; i < iterations; i++) | |
instances[i] = lazies[i].Value; | |
watch.Stop(); | |
Console.WriteLine("Lazy using func finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory4<T>(Func<T> func) | |
{ | |
return new Lazy<T>(func); | |
} | |
} |
This file contains hidden or 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
public class Test1 | |
{ | |
private const int iterations = 10000000; | |
public static void Main() | |
{ | |
Test1.Create1<Class1>(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCApproach(); | |
GC.WaitForFullGCComplete(); | |
Test1.Create2<Class1>(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCApproach(); | |
GC.WaitForFullGCComplete(); | |
Test1.Create3<Class1>(); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.WaitForFullGCApproach(); | |
GC.WaitForFullGCComplete(); | |
Test1.Create4<Class1>(() => new Class1()); | |
} | |
public static void Create1<T>() where T : new() | |
{ | |
Stopwatch watch = new Stopwatch(); | |
Lazy<T>[] lazies = new Lazy<T>[10000000]; | |
T[] instances = new T[10000000]; | |
Console.WriteLine("Lazy using generic"); | |
watch.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
lazies[i] = Test1.Factory1<T>(); | |
} | |
for (int j = 0; j < 10000000; j++) | |
{ | |
instances[j] = lazies[j].Value; | |
} | |
watch.Stop(); | |
Console.WriteLine("Lazy using generic finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory1<T>() where T : new() | |
{ | |
return new Lazy<T>(delegate | |
{ | |
if (default(T) != null) | |
{ | |
return default(T); | |
} | |
return Activator.CreateInstance<T>(); | |
}); | |
} | |
public static void Create2<T>() where T : new() | |
{ | |
Stopwatch watch = new Stopwatch(); | |
Lazy<T>[] lazies = new Lazy<T>[10000000]; | |
T[] instances = new T[10000000]; | |
Console.WriteLine("Lazy using activator"); | |
watch.Reset(); | |
watch.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
lazies[i] = Test1.Factory2<T>(); | |
} | |
for (int j = 0; j < 10000000; j++) | |
{ | |
instances[j] = lazies[j].Value; | |
} | |
watch.Stop(); | |
Console.WriteLine("Lazy using activator finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory2<T>() where T : new() | |
{ | |
return new Lazy<T>(() => (T)((object)Activator.CreateInstance(typeof(T)))); | |
} | |
public static void Create3<T>() where T : new() | |
{ | |
Stopwatch watch = new Stopwatch(); | |
Lazy<T>[] lazies = new Lazy<T>[10000000]; | |
T[] instances = new T[10000000]; | |
Console.WriteLine("Lazy using generic activator"); | |
watch.Reset(); | |
watch.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
lazies[i] = Test1.Factory3<T>(); | |
} | |
for (int j = 0; j < 10000000; j++) | |
{ | |
instances[j] = lazies[j].Value; | |
} | |
watch.Stop(); | |
Console.WriteLine("Lazy using generic activator finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory3<T>() where T : new() | |
{ | |
return new Lazy<T>(() => Activator.CreateInstance<T>()); | |
} | |
public static void Create4<T>(Func<T> func) | |
{ | |
Stopwatch watch = new Stopwatch(); | |
Lazy<T>[] lazies = new Lazy<T>[10000000]; | |
T[] instances = new T[10000000]; | |
Console.WriteLine("Lazy using func"); | |
watch.Reset(); | |
watch.Start(); | |
for (int i = 0; i < 10000000; i++) | |
{ | |
lazies[i] = Test1.Factory4<T>(func); | |
} | |
for (int j = 0; j < 10000000; j++) | |
{ | |
instances[j] = lazies[j].Value; | |
} | |
watch.Stop(); | |
Console.WriteLine("Lazy using func finished in " + watch.ElapsedMilliseconds); | |
} | |
private static Lazy<T> Factory4<T>(Func<T> func) | |
{ | |
return new Lazy<T>(func); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment