Last active
August 29, 2015 14:18
-
-
Save GregRos/b7511ffacac7ebfab12c 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.Diagnostics; | |
using System.Threading; | |
using System.Runtime.CompilerServices; | |
using System.Linq; | |
namespace Tests | |
{ | |
internal class Bench { | |
public int Drops = 5; | |
public int Runs = 10; | |
public int MsTimeout = 20000; | |
public Stopwatch Watch = new Stopwatch(); | |
public Action OnRun = () => { | |
//GC.Collect(2,GCCollectionMode.Forced, true); | |
//GC.WaitForPendingFinalizers(); | |
}; | |
public double InvokeTest(Action act) { | |
Action runner = () => { | |
for (int i = 0; i < Drops; i++) { | |
act(); | |
} | |
Watch.Reset(); | |
OnRun(); | |
Watch.Start(); | |
for (int i = 0; i < Runs; i++) { | |
act(); | |
} | |
Watch.Stop(); | |
}; | |
var thread = new Thread(() => runner()); | |
thread.Start(); | |
var success = thread.Join(MsTimeout); | |
return success ? Watch.Elapsed.TotalMilliseconds/Runs : -1; | |
} | |
} | |
internal static class GenericObjectStaticConstructor<T> | |
{ | |
static GenericObjectStaticConstructor() { | |
} | |
public static void SeemsVerySimple(int iterations) | |
{ | |
Func<int, int> f = x => x; | |
var z = 0; | |
for (int i = 0; i < iterations; i++) | |
{ | |
z = f(z); | |
} | |
} | |
private static int DoNothing(int x) { | |
return x; | |
} | |
public static void SeemsVerySimple2(int iterations) | |
{ | |
Func<int, int> f = x => x; | |
var z = 0; | |
for (int i = 0; i < iterations; i++) | |
{ | |
z = DoNothing(z); | |
} | |
} | |
public static void NothingAtAll() {} | |
public static void SeemsVerySimple3(int iterations) | |
{ | |
var z = 0; | |
for (int i = 0; i < iterations; i++) | |
{ | |
NothingAtAll(); | |
} | |
} | |
} | |
internal static class GenericObjectNoStaticConstructor<T> | |
{ | |
public static int[] initializer = Enumerable.Range(0,100000).ToArray(); | |
public static void SeemsVerySimple(int iterations) | |
{ | |
Func<int, int> f = x => x; | |
var z = 0; | |
for (int i = 0; i < iterations; i++) | |
{ | |
z = f(z); | |
} | |
} | |
private static int DoNothing(int x) { | |
return x; | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
private static void DoNothingAtAll() { | |
} | |
public static void SeemsVerySimple2(int iterations) | |
{ | |
var z = 0; | |
for (int i = 0; i < iterations; i++) | |
{ | |
DoNothingAtAll(); | |
} | |
} | |
} | |
public class Program | |
{ | |
public static void Main (String[] args) | |
{ | |
var bench = new Bench(); | |
var time1 = bench.InvokeTest(() => GenericObjectStaticConstructor<string>.SeemsVerySimple(1000000)); | |
var time2 = bench.InvokeTest(() => GenericObjectStaticConstructor<int>.SeemsVerySimple(1000000)); | |
var time3 = bench.InvokeTest(() => GenericObjectNoStaticConstructor<string>.SeemsVerySimple(1000000)); | |
var time4 = bench.InvokeTest(() => GenericObjectNoStaticConstructor<int>.SeemsVerySimple(1000000)); | |
var time5 = bench.InvokeTest(() => GenericObjectStaticConstructor<string>.SeemsVerySimple2(1000000)); | |
var time6 = bench.InvokeTest(() => GenericObjectStaticConstructor<int>.SeemsVerySimple2(1000000)); | |
var time7 = bench.InvokeTest(() => GenericObjectNoStaticConstructor<string>.SeemsVerySimple2(1000000)); | |
var time8 = bench.InvokeTest(() => GenericObjectNoStaticConstructor<int>.SeemsVerySimple2(1000000)); | |
Console.WriteLine("This is with a locally defined lambda:"); | |
Console.WriteLine("Static Constructor<String>: {0}, Static Constructor<int>: {1}, NoConstructor<string>: {2}, NoConstructor<int>: {3}", time1, time2, time3, time4); | |
Console.WriteLine("This is with a static method call:"); | |
Console.WriteLine("Static Constructor<String>: {0}, Static Constructor<int>: {1}, NoConstructor<string>: {2}, NoConstructor<int>: {3}", time5, time6, time7, time8); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment