andrew4582

SingletonBenchmark

Aug 6th, 2010
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2.  
  3. public class SingletonBenchmark
  4. {
  5.     const int Iterations = 1000000000;
  6.    
  7.     static DateTime timer;
  8.    
  9.     static void Main()
  10.     {        
  11.         ResetTimer(null);
  12.        
  13.         for (int i=0; i < Iterations; i++)
  14.         {
  15.             SimpleLock sl = SimpleLock.Instance;
  16.         }
  17.         ResetTimer("SimpleLock");
  18.        
  19.         for (int i=0; i < Iterations; i++)
  20.         {
  21.             FixedDcl dcl = FixedDcl.Instance;
  22.         }
  23.         ResetTimer("FixedDcl");
  24.  
  25.         for (int i=0; i < Iterations; i++)
  26.         {
  27.             BrokenDcl dcl = BrokenDcl.Instance;
  28.         }
  29.         ResetTimer("BrokenDcl");
  30.  
  31.         for (int i=0; i < Iterations; i++)
  32.         {
  33.             SimpleNoLockLazy inst = SimpleNoLockLazy.Instance;
  34.         }
  35.         ResetTimer("SimpleNoLockLazy");
  36.  
  37.         for (int i=0; i < Iterations; i++)
  38.         {
  39.             SimpleNoLockNotAsLazy inst = SimpleNoLockNotAsLazy.Instance;
  40.         }
  41.         ResetTimer("SimpleNoLockNotAsLazy");
  42.        
  43.         for (int i=0; i < Iterations; i++)
  44.         {
  45.             NestedLazy nl = NestedLazy.Instance;
  46.         }
  47.         ResetTimer("NestedLazy");
  48.  
  49.         for (int i=0; i < Iterations; i++)
  50.         {
  51.             NestedNotAsLazy nl = NestedNotAsLazy.Instance;
  52.         }
  53.         ResetTimer("NestedNotAsLazy");
  54.        
  55.     }
  56.    
  57.     static void ResetTimer (string description)
  58.     {
  59.         DateTime now = DateTime.UtcNow;
  60.         if (description != null)
  61.         {
  62.             Console.WriteLine ("{0} took {1}", description, now-timer);
  63.         }
  64.         timer = DateTime.UtcNow;
  65.     }
  66. }
  67.  
  68. public sealed class SimpleLock
  69. {
  70.     static SimpleLock instance=null;
  71.     static readonly object padlock = new object();
  72.  
  73.     SimpleLock()
  74.     {
  75.     }
  76.  
  77.     public static SimpleLock Instance
  78.     {
  79.         get
  80.         {
  81.             lock (padlock)
  82.             {
  83.                 if (instance==null)
  84.                 {
  85.                     instance = new SimpleLock();
  86.                 }
  87.                 return instance;
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93. public sealed class BrokenDcl
  94. {
  95.     static BrokenDcl instance=null;
  96.     static readonly object padlock = new object();
  97.  
  98.     BrokenDcl()
  99.     {
  100.     }
  101.  
  102.     public static BrokenDcl Instance
  103.     {
  104.         get
  105.         {
  106.             if (instance==null)
  107.             {
  108.                 lock (padlock)
  109.                 {
  110.                     if (instance==null)
  111.                     {
  112.                         instance = new BrokenDcl();
  113.                     }
  114.                 }
  115.             }
  116.             return instance;
  117.         }
  118.     }
  119. }
  120.  
  121. public sealed class FixedDcl
  122. {
  123.     static volatile FixedDcl instance=null;
  124.     static readonly object padlock = new object();
  125.  
  126.     FixedDcl()
  127.     {
  128.     }
  129.  
  130.     public static FixedDcl Instance
  131.     {
  132.         get
  133.         {
  134.             if (instance==null)
  135.             {
  136.                 lock (padlock)
  137.                 {
  138.                     if (instance==null)
  139.                     {
  140.                         instance = new FixedDcl();
  141.                     }
  142.                 }
  143.             }
  144.             return instance;
  145.         }
  146.     }
  147. }
  148.  
  149. public sealed class SimpleNoLockLazy
  150. {
  151.     static readonly SimpleNoLockLazy instance=new SimpleNoLockLazy();
  152.  
  153.     // Explicit static constructor to tell C# compiler
  154.     // not to mark type as beforefieldinit
  155.     static SimpleNoLockLazy()
  156.     {
  157.     }
  158.  
  159.     SimpleNoLockLazy()
  160.     {
  161.     }
  162.  
  163.     public static SimpleNoLockLazy Instance
  164.     {
  165.         get
  166.         {
  167.             return instance;
  168.         }
  169.     }
  170. }
  171.  
  172. public sealed class SimpleNoLockNotAsLazy
  173. {
  174.     static readonly SimpleNoLockNotAsLazy instance=new SimpleNoLockNotAsLazy();
  175.  
  176.     SimpleNoLockNotAsLazy()
  177.     {
  178.     }
  179.  
  180.     public static SimpleNoLockNotAsLazy Instance
  181.     {
  182.         get
  183.         {
  184.             return instance;
  185.         }
  186.     }
  187. }
  188.  
  189. public sealed class NestedLazy
  190. {
  191.     NestedLazy()
  192.     {
  193.     }
  194.  
  195.     public static NestedLazy Instance
  196.     {
  197.         get
  198.         {
  199.             return Nested.instance;
  200.         }
  201.     }
  202.    
  203.     class Nested
  204.     {
  205.         // Explicit static constructor to tell C# compiler
  206.         // not to mark type as beforefieldinit
  207.         static Nested()
  208.         {
  209.         }
  210.  
  211.         internal static readonly NestedLazy instance = new NestedLazy();
  212.     }
  213. }
  214.  
  215. public sealed class NestedNotAsLazy
  216. {
  217.     NestedNotAsLazy()
  218.     {
  219.     }
  220.  
  221.     public static NestedNotAsLazy Instance
  222.     {
  223.         get
  224.         {
  225.             return Nested.instance;
  226.         }
  227.     }
  228.    
  229.     class Nested
  230.     {
  231.         internal static readonly NestedNotAsLazy instance = new NestedNotAsLazy();
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment