Advertisement
Guest User

Untitled

a guest
Sep 7th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.18 KB | None | 0 0
  1.     static class Methods
  2.     {
  3.         public static double Fun1() => Math.PI;
  4.         public static double Fun2() => Math.E;
  5.         public static double Fun3() => Math.Tau;
  6.         public static double Fun4() => 273.15;
  7.         public static double Fun5() => 1;
  8.         public static double Fun6() => 2300;
  9.         public static double Fun7() => 0.1;
  10.         public static double Fun8() => 100;
  11.         public static double Fun9() => 1987;
  12.         public static double Fun10() => 450;
  13.     }
  14.    
  15.    
  16.  
  17.     interface IInterface
  18.     {
  19.         public static IInterface Get_random_implementation()
  20.         {
  21.             int random_index = Random.Shared.Next(0, 10);
  22.  
  23.             switch (random_index)
  24.             {
  25.                 case 0: return new Impl1();
  26.                 case 1: return new Impl2();
  27.                 case 2: return new Impl3();
  28.                 case 3: return new Impl4();
  29.                 case 4: return new Impl5();
  30.                 case 5: return new Impl6();
  31.                 case 6: return new Impl7();
  32.                 case 7: return new Impl8();
  33.                 case 8: return new Impl9();
  34.                 case 9: return new Impl10();
  35.                 default: throw new Exception();
  36.             }
  37.         }
  38.  
  39.         double Get_value();
  40.     }
  41.  
  42.  
  43.     class Impl1 : IInterface { public double Get_value() => Methods.Fun1(); }
  44.  
  45.     class Impl2 : IInterface { public double Get_value() => Methods.Fun2(); }
  46.  
  47.     class Impl3 : IInterface { public double Get_value() => Methods.Fun3(); }
  48.  
  49.     class Impl4 : IInterface { public double Get_value() => Methods.Fun4(); }
  50.  
  51.     class Impl5 : IInterface { public double Get_value() => Methods.Fun5(); }
  52.  
  53.     class Impl6 : IInterface { public double Get_value() => Methods.Fun6(); }
  54.  
  55.     class Impl7 : IInterface { public double Get_value() => Methods.Fun7(); }
  56.  
  57.     class Impl8 : IInterface { public double Get_value() => Methods.Fun8(); }
  58.  
  59.     class Impl9 : IInterface { public double Get_value() => Methods.Fun9(); }
  60.  
  61.     class Impl10 : IInterface { public double Get_value() => Methods.Fun10(); }
  62.  
  63.  
  64.  
  65.  
  66.     public class Tester
  67.     {
  68.         public static void Run()
  69.         {
  70.             Run_with_BenchmarkDotNet();
  71.             //Run_with_manual_swopwatch();
  72.         }
  73.  
  74.         public static void Run_with_BenchmarkDotNet()
  75.         {
  76.             BenchmarkDotNet.Reports.Summary? summary = BenchmarkRunner.Run<Tester>();
  77.         }
  78.  
  79.  
  80.         //public static void Run_with_manual_swopwatch()
  81.         //{
  82.         //    Stopwatch sw = new();
  83.         //    Tester tester = new();
  84.  
  85.  
  86.         //    int nit = 50;
  87.  
  88.         //    Call("Test_interface", tester.Test_interface, nit);
  89.         //    Call("Test_multiple_ifs", tester.Test_multiple_ifs, nit);
  90.         //    Call("Test_swith", tester.Test_swith, nit);
  91.         //    Call("Test_arr", tester.Test_arr, nit);
  92.         //    Call("Test_delegate_array", tester.Test_delegate_array, nit);
  93.         //}
  94.  
  95.  
  96.         //static void Call(string name, Func<double> method, int nit)
  97.         //{
  98.         //    Stopwatch sw = new();
  99.         //    sw.Start();
  100.         //    double sum = 0;
  101.         //    for (int i = 0; i < nit; i++)
  102.         //    {
  103.         //        sum = method();
  104.         //    }
  105.         //    sw.Stop();
  106.         //    double dur = sw.Elapsed.TotalMilliseconds / nit;
  107.         //    Console.WriteLine($"{name}. Sum = {sum:N0}. Time per oper = {dur:N1} ms");
  108.         //}
  109.  
  110.  
  111.  
  112.  
  113.         const int m = 10_000;
  114.         int[] random_indexes = new int[m];
  115.         IInterface[] interface_implementations = new IInterface[m];
  116.         double[] values = new double[m];
  117.         Func<double>[] delegates = new Func<double>[m];
  118.  
  119.  
  120.         public Tester()
  121.         {
  122.             for (int i = 0; i < m; i++)
  123.             {
  124.                 random_indexes[i] = Random.Shared.Next(0, 10);
  125.                 interface_implementations[i] = IInterface.Get_random_implementation();
  126.                 values[i] = Get_value(Random.Shared.Next(0, 10));
  127.                 delegates[i] = Get_delegate(Random.Shared.Next(0, 10));
  128.             }
  129.         }
  130.  
  131.  
  132.         public double Get_value(int random_index)
  133.         {
  134.             if (random_index == 0) return Methods.Fun1();
  135.             else if (random_index == 1) return Methods.Fun2();
  136.             else if (random_index == 2) return Methods.Fun3();
  137.             else if (random_index == 3) return Methods.Fun4();
  138.             else if (random_index == 4) return Methods.Fun5();
  139.             else if (random_index == 5) return Methods.Fun6();
  140.             else if (random_index == 6) return Methods.Fun7();
  141.             else if (random_index == 7) return Methods.Fun8();
  142.             else if (random_index == 8) return Methods.Fun9();
  143.             else if (random_index == 9) return Methods.Fun10();
  144.             else throw new Exception();
  145.         }
  146.  
  147.         public Func<double> Get_delegate(int random_index)
  148.         {
  149.             if (random_index == 0) return Methods.Fun1;
  150.             else if (random_index == 1) return Methods.Fun2;
  151.             else if (random_index == 2) return Methods.Fun3;
  152.             else if (random_index == 3) return Methods.Fun4;
  153.             else if (random_index == 4) return Methods.Fun5;
  154.             else if (random_index == 5) return Methods.Fun6;
  155.             else if (random_index == 6) return Methods.Fun7;
  156.             else if (random_index == 7) return Methods.Fun8;
  157.             else if (random_index == 8) return Methods.Fun9;
  158.             else if (random_index == 9) return Methods.Fun10;
  159.             else throw new Exception();
  160.         }
  161.  
  162.  
  163.         [Benchmark]
  164.         public double Test_interface()
  165.         {
  166.             double sum = 0;
  167.             for (int i = 0; i < m; i++)
  168.             {
  169.                 sum += interface_implementations[i].Get_value();
  170.             }
  171.  
  172.             return sum;
  173.         }
  174.  
  175.  
  176.         [Benchmark]
  177.         public double Test_multiple_ifs()
  178.         {
  179.             double sum = 0;
  180.             for (int i = 0; i < m; i++)
  181.             {
  182.                 int random_index = random_indexes[i];
  183.                 if (random_index == 0) sum += Methods.Fun1();
  184.                 else if (random_index == 1) sum += Methods.Fun2();
  185.                 else if (random_index == 2) sum += Methods.Fun3();
  186.                 else if (random_index == 3) sum += Methods.Fun4();
  187.                 else if (random_index == 4) sum += Methods.Fun5();
  188.                 else if (random_index == 5) sum += Methods.Fun6();
  189.                 else if (random_index == 6) sum += Methods.Fun7();
  190.                 else if (random_index == 7) sum += Methods.Fun8();
  191.                 else if (random_index == 8) sum += Methods.Fun9();
  192.                 else if (random_index == 9) sum += Methods.Fun10();
  193.                 else throw new Exception();
  194.             }
  195.  
  196.             return sum;
  197.         }
  198.  
  199.  
  200.  
  201.         [Benchmark]
  202.         public double Test_swith()
  203.         {
  204.             double sum = 0;
  205.             for (int i = 0; i < m; i++)
  206.             {
  207.                 switch (random_indexes[i])
  208.                 {
  209.                     case 0: sum += Methods.Fun1(); break;
  210.                     case 1: sum += Methods.Fun2(); break;
  211.                     case 2: sum += Methods.Fun3(); break;
  212.                     case 3: sum += Methods.Fun4(); break;
  213.                     case 4: sum += Methods.Fun5(); break;
  214.                     case 5: sum += Methods.Fun6(); break;
  215.                     case 6: sum += Methods.Fun7(); break;
  216.                     case 7: sum += Methods.Fun8(); break;
  217.                     case 8: sum += Methods.Fun9(); break;
  218.                     case 9: sum += Methods.Fun10(); break;
  219.                     default: throw new Exception();
  220.                 }
  221.             }
  222.  
  223.             return sum;
  224.         }
  225.  
  226.  
  227.         [Benchmark]
  228.         public double Test_arr()
  229.         {
  230.             double sum = 0;
  231.             for (int i = 0; i < m; i++)
  232.             {
  233.                 sum += values[i];
  234.             }
  235.  
  236.             return sum;
  237.         }
  238.  
  239.  
  240.         [Benchmark]
  241.         public double Test_delegate_array()
  242.         {
  243.             double sum = 0;
  244.             for (int i = 0; i < m; i++)
  245.             {
  246.                 sum += delegates[i]();
  247.             }
  248.  
  249.             return sum;
  250.         }
  251.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement