SHARE
TWEET

More static constructors

a guest Nov 16th, 2011 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public struct NoStaticCtor
  2.     {
  3.         private static int _myValue = 3;
  4.         public static int GetMyValue() { return _myValue; }
  5.     }
  6.  
  7.     public struct StaticCtor
  8.     {
  9.         private static int _myValue;
  10.         public static int GetMyValue() { return _myValue; }
  11.         static StaticCtor()
  12.         {
  13.             _myValue = 3;
  14.         }
  15.     }
  16.  
  17.     class Program
  18.     {
  19.         private static readonly long numTimes = 5000000000;
  20.         static void DoFirstLoop()
  21.         {
  22.             Stopwatch sw = new Stopwatch();
  23.             sw.Start();
  24.             for (long i = 0; i < numTimes; i++)
  25.             {
  26.                 NoStaticCtor.GetMyValue();
  27.             }
  28.             sw.Stop();
  29.             Console.WriteLine("First Loop: No static ctor: {0}", sw.Elapsed);
  30.  
  31.             sw.Restart();
  32.             for (long i = 0; i < numTimes; i++)
  33.             {
  34.                 StaticCtor.GetMyValue();
  35.             }
  36.             sw.Stop();
  37.             Console.WriteLine("First Loop: with static ctor: {0}", sw.Elapsed);
  38.         }
  39.  
  40.         static void DoSecondLoop()
  41.         {
  42.             Stopwatch sw = new Stopwatch();
  43.             sw.Start();
  44.             for (long i = 0; i < numTimes; i++)
  45.             {
  46.                 NoStaticCtor.GetMyValue();
  47.             }
  48.             sw.Stop();
  49.             Console.WriteLine("Second Loop: No static ctor: {0}", sw.Elapsed);
  50.  
  51.             sw.Restart();
  52.             for (long i = 0; i < numTimes; i++)
  53.             {
  54.                 StaticCtor.GetMyValue();
  55.             }
  56.             sw.Stop();
  57.             Console.WriteLine("Second Loop: with static ctor: {0}", sw.Elapsed);
  58.         }
  59.  
  60.         static void Main(string[] args)
  61.         {
  62.             DoFirstLoop(); // Slow, because JITer adds the check to the loop
  63.             DoFirstLoop(); // Still slow, because the JIT uses the cached method
  64.             DoSecondLoop(); // Fast, because the static constructor was called in the first function, no checks added when JITed
  65.         }
  66.     }
  67. --------------
  68. First Loop: No static ctor: 00:00:03.5460631
  69. First Loop: with static ctor: 00:00:14.6650061
  70. First Loop: No static ctor: 00:00:03.2156631
  71. First Loop: with static ctor: 00:00:14.5452244
  72. Second Loop: No static ctor: 00:00:03.2322878
  73. Second Loop: with static ctor: 00:00:03.2336642
  74. Press any key to continue . . .
  75.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top