using System; public class SingletonBenchmark { const int Iterations = 1000000000; static DateTime timer; static void Main() { ResetTimer(null); for (int i=0; i < Iterations; i++) { SimpleLock sl = SimpleLock.Instance; } ResetTimer("SimpleLock"); for (int i=0; i < Iterations; i++) { FixedDcl dcl = FixedDcl.Instance; } ResetTimer("FixedDcl"); for (int i=0; i < Iterations; i++) { BrokenDcl dcl = BrokenDcl.Instance; } ResetTimer("BrokenDcl"); for (int i=0; i < Iterations; i++) { SimpleNoLockLazy inst = SimpleNoLockLazy.Instance; } ResetTimer("SimpleNoLockLazy"); for (int i=0; i < Iterations; i++) { SimpleNoLockNotAsLazy inst = SimpleNoLockNotAsLazy.Instance; } ResetTimer("SimpleNoLockNotAsLazy"); for (int i=0; i < Iterations; i++) { NestedLazy nl = NestedLazy.Instance; } ResetTimer("NestedLazy"); for (int i=0; i < Iterations; i++) { NestedNotAsLazy nl = NestedNotAsLazy.Instance; } ResetTimer("NestedNotAsLazy"); } static void ResetTimer (string description) { DateTime now = DateTime.UtcNow; if (description != null) { Console.WriteLine ("{0} took {1}", description, now-timer); } timer = DateTime.UtcNow; } } public sealed class SimpleLock { static SimpleLock instance=null; static readonly object padlock = new object(); SimpleLock() { } public static SimpleLock Instance { get { lock (padlock) { if (instance==null) { instance = new SimpleLock(); } return instance; } } } } public sealed class BrokenDcl { static BrokenDcl instance=null; static readonly object padlock = new object(); BrokenDcl() { } public static BrokenDcl Instance { get { if (instance==null) { lock (padlock) { if (instance==null) { instance = new BrokenDcl(); } } } return instance; } } } public sealed class FixedDcl { static volatile FixedDcl instance=null; static readonly object padlock = new object(); FixedDcl() { } public static FixedDcl Instance { get { if (instance==null) { lock (padlock) { if (instance==null) { instance = new FixedDcl(); } } } return instance; } } } public sealed class SimpleNoLockLazy { static readonly SimpleNoLockLazy instance=new SimpleNoLockLazy(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static SimpleNoLockLazy() { } SimpleNoLockLazy() { } public static SimpleNoLockLazy Instance { get { return instance; } } } public sealed class SimpleNoLockNotAsLazy { static readonly SimpleNoLockNotAsLazy instance=new SimpleNoLockNotAsLazy(); SimpleNoLockNotAsLazy() { } public static SimpleNoLockNotAsLazy Instance { get { return instance; } } } public sealed class NestedLazy { NestedLazy() { } public static NestedLazy Instance { get { return Nested.instance; } } class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly NestedLazy instance = new NestedLazy(); } } public sealed class NestedNotAsLazy { NestedNotAsLazy() { } public static NestedNotAsLazy Instance { get { return Nested.instance; } } class Nested { internal static readonly NestedNotAsLazy instance = new NestedNotAsLazy(); } }