Advertisement
GeneralGDA

Is "in" worth it? №2

Dec 15th, 2021
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Immutable;
  3. using BenchmarkDotNet.Attributes;
  4. using BenchmarkDotNet.Jobs;
  5. using BenchmarkDotNet.Running;
  6.  
  7. namespace Benchmark
  8. {
  9.     [SimpleJob(RuntimeMoniker.CoreRt50)]
  10.     public class Md5VsSha256
  11.     {
  12.         readonly struct Heavy
  13.         {
  14.             public double A { init; get; }
  15.             public double B { init; get; }
  16.             public double C { init; get; }
  17.             public double D { init; get; }
  18.             public double E { init; get; }
  19.         }
  20.  
  21.         private Heavy data;
  22.  
  23.         [GlobalSetup]
  24.         public void Setup()
  25.         {
  26.             var random = new Random();
  27.  
  28.             data = new Heavy
  29.             {
  30.                 A = random.NextDouble(),
  31.                 B = random.NextDouble(),
  32.                 C = random.NextDouble(),
  33.                 D = random.NextDouble(),
  34.                 E = random.NextDouble(),
  35.             };
  36.         }
  37.  
  38.         static double ViaRef(in Heavy array)
  39.         {
  40.             double  sum
  41.                 = array.A +
  42.                   array.B +
  43.                   array.C +
  44.                   array.D +
  45.                   array.E
  46.                   ;
  47.             return sum;
  48.         }
  49.  
  50.         static double ViaValue(Heavy array)
  51.         {
  52.             double  sum
  53.                     = array.A +
  54.                       array.B +
  55.                       array.C +
  56.                       array.D +
  57.                       array.E
  58.                 ;
  59.             return sum;
  60.         }
  61.  
  62.         [Benchmark]
  63.         public double Ref()
  64.         {
  65.             return ViaRef(data);
  66.         }
  67.  
  68.         [Benchmark]
  69.         public double Value()
  70.         {
  71.             return ViaValue(data);
  72.         }
  73.     }
  74.  
  75.     public static class Program
  76.     {
  77.         public static void Main()
  78.         {
  79.             BenchmarkRunner.Run<Md5VsSha256>();
  80.         }
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement