Advertisement
Guest User

Micro-Benchmark Readonly fields

a guest
Sep 10th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. /// <summary>
  5. /// Struct with 4 readonly-fields
  6. /// </summary>
  7. public struct Int256
  8. {
  9.     private readonly long bits0;
  10.     private readonly long bits1;
  11.     private readonly long bits2;
  12.     private readonly long bits3;
  13.  
  14.     public Int256(long bits0, long bits1, long bits2, long bits3)
  15.     {
  16.         this.bits0 = bits0; this.bits1 = bits1; this.bits2 = bits2; this.bits3 = bits3;
  17.     }
  18.  
  19.     public long Bits0 { get { return bits0; } }
  20.     public long Bits1 { get { return bits1; } }
  21.     public long Bits2 { get { return bits2; } }
  22.     public long Bits3 { get { return bits3; } }
  23. }
  24.  
  25. /// <summary>
  26. /// Int256 variant with non-readonly fields
  27. /// </summary>
  28. public struct Int256_NRO
  29. {
  30.     private long bits0;
  31.     private long bits1;
  32.     private long bits2;
  33.     private long bits3;
  34.  
  35.     public Int256_NRO(long bits0, long bits1, long bits2, long bits3)
  36.     {
  37.         this.bits0 = bits0; this.bits1 = bits1; this.bits2 = bits2; this.bits3 = bits3;
  38.     }
  39.  
  40.     public long Bits0 { get { return bits0; } }
  41.     public long Bits1 { get { return bits1; } }
  42.     public long Bits2 { get { return bits2; } }
  43.     public long Bits3 { get { return bits3; } }
  44.  
  45.     public long TotalValueOnStruct { get { return Bits0 + Bits1 + Bits2 + Bits3; } }
  46. }
  47.  
  48. class Test
  49. {
  50.     private readonly Int256     value;
  51.     private readonly Int256_NRO value2;
  52.  
  53.     /* non-readonly variants */
  54.  
  55.     private Int256     value3;
  56.     private Int256_NRO value4;
  57.  
  58.  
  59.     public Test()
  60.     {
  61.         value  = new Int256    (1L, 5L, 10L, 100L);
  62.         value2 = new Int256_NRO(1L, 5L, 10L, 100L);
  63.  
  64.         /* non-readonly variants */
  65.  
  66.         value3 = new Int256    (1L, 5L, 10L, 100L);
  67.         value4 = new Int256_NRO(1L, 5L, 10L, 100L);
  68.     }
  69.  
  70.     public long TotalValue {
  71.         get { return value.Bits0 + value.Bits1 + value.Bits2 + value.Bits3; }
  72.     }
  73.  
  74.     public long TotalValue2 {
  75.         get { return value2.Bits0 + value2.Bits1 + value2.Bits2 + value2.Bits3; }
  76.     }
  77.  
  78.     public long TotalValue3 {
  79.         get { return value3.Bits0 + value3.Bits1 + value3.Bits2 + value3.Bits3; }
  80.     }
  81.  
  82.     public long TotalValue4 {
  83.         get { return value4.Bits0 + value4.Bits1 + value4.Bits2 + value4.Bits3; }
  84.     }
  85.  
  86.     public void RunTest()
  87.     {
  88.         const int numIterations = 100000000;
  89.  
  90.         // Just make sure it's JITted…
  91.         long sample = TotalValue; sample = TotalValue2; sample = TotalValue3; sample = TotalValue4;
  92.  
  93.         long total;
  94.  
  95.         Stopwatch sw = Stopwatch.StartNew(); total = 0;
  96.         for (int i = 0; i < numIterations; i++) total += TotalValue;
  97.         sw.Stop();
  98.         Console.WriteLine("{1,-60} {0,5} ms", sw.ElapsedMilliseconds, "Readonly value, readonly fields:");
  99.  
  100.         sw.Reset(); sw.Start(); total = 0;
  101.         for (int i = 0; i < numIterations; i++) total += TotalValue2;
  102.         sw.Stop();
  103.         Console.WriteLine("{1,-60} {0,5} ms", sw.ElapsedMilliseconds, "Readonly value, non-readonly fields:");
  104.  
  105.  
  106.         sw.Reset(); sw.Start(); total = 0;
  107.         for (int i = 0; i < numIterations; i++) total += value2.TotalValueOnStruct;
  108.         sw.Stop();
  109.         Console.WriteLine("{1,-60} {0,5} ms", sw.ElapsedMilliseconds, "Readonly value, non-readonly fields, Total on structure:");
  110.  
  111.  
  112.         /* now the non-readonly 'value' versions value3 and value4*/
  113.  
  114.         sw.Reset(); sw.Start(); total = 0;
  115.         for (int i = 0; i < numIterations; i++) total += TotalValue3;
  116.         sw.Stop();
  117.         Console.WriteLine("{1,-60} {0,5} ms", sw.ElapsedMilliseconds, "Non-Readonly value, readonly fields:");
  118.  
  119.  
  120.         sw.Reset(); sw.Start(); total = 0;
  121.         for (int i = 0; i < numIterations; i++) total += TotalValue4;
  122.         sw.Stop();
  123.         Console.WriteLine("{1,-60} {0,5} ms", sw.ElapsedMilliseconds, "Non-Readonly value, non-readonly fields:");
  124.  
  125.         sw.Reset(); sw.Start(); total = 0;
  126.         for (int i = 0; i < numIterations; i++) total += value4.TotalValueOnStruct;
  127.         sw.Stop();
  128.         Console.WriteLine("{1,-60} {0,5} ms", sw.ElapsedMilliseconds, "Non-Readonly value, non-readonly fields, Total on structure:");
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement