using System.Diagnostics;
using System.Runtime.CompilerServices;
public int MsTimeout = 20000;
public Stopwatch Watch = new Stopwatch();
public Action OnRun = () => {
public double InvokeTest(Action act) {
for (int i = 0; i < Drops; i++) {
for (int i = 0; i < Runs; i++) {
var thread = new Thread(() => runner());
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;
for (int i = 0; i < iterations; i++)
private static int DoNothing(int x) {
public static void SeemsVerySimple2(int iterations)
Func<int, int> f = x => x;
for (int i = 0; i < iterations; i++)
[MethodImpl(MethodImplOptions.NoInlining)]
public static void NothingAtAll() {}
public static void SeemsVerySimple3(int iterations)
for (int i = 0; i < iterations; i++)
public 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;
for (int i = 0; i < iterations; i++)
private static int DoNothing(int x) {
[MethodImpl(MethodImplOptions.NoInlining)]
private static void DoNothingAtAll() {
public static void SeemsVerySimple2(int iterations)
for (int i = 0; i < iterations; i++)
public static void Main (String[] args)
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);