Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Methods
- {
- public static double Fun1() => Math.PI;
- public static double Fun2() => Math.E;
- public static double Fun3() => Math.Tau;
- public static double Fun4() => 273.15;
- public static double Fun5() => 1;
- public static double Fun6() => 2300;
- public static double Fun7() => 0.1;
- public static double Fun8() => 100;
- public static double Fun9() => 1987;
- public static double Fun10() => 450;
- }
- interface IInterface
- {
- public static IInterface Get_random_implementation()
- {
- int random_index = Random.Shared.Next(0, 10);
- switch (random_index)
- {
- case 0: return new Impl1();
- case 1: return new Impl2();
- case 2: return new Impl3();
- case 3: return new Impl4();
- case 4: return new Impl5();
- case 5: return new Impl6();
- case 6: return new Impl7();
- case 7: return new Impl8();
- case 8: return new Impl9();
- case 9: return new Impl10();
- default: throw new Exception();
- }
- }
- double Get_value();
- }
- class Impl1 : IInterface { public double Get_value() => Methods.Fun1(); }
- class Impl2 : IInterface { public double Get_value() => Methods.Fun2(); }
- class Impl3 : IInterface { public double Get_value() => Methods.Fun3(); }
- class Impl4 : IInterface { public double Get_value() => Methods.Fun4(); }
- class Impl5 : IInterface { public double Get_value() => Methods.Fun5(); }
- class Impl6 : IInterface { public double Get_value() => Methods.Fun6(); }
- class Impl7 : IInterface { public double Get_value() => Methods.Fun7(); }
- class Impl8 : IInterface { public double Get_value() => Methods.Fun8(); }
- class Impl9 : IInterface { public double Get_value() => Methods.Fun9(); }
- class Impl10 : IInterface { public double Get_value() => Methods.Fun10(); }
- public class Tester
- {
- public static void Run()
- {
- Run_with_BenchmarkDotNet();
- //Run_with_manual_swopwatch();
- }
- public static void Run_with_BenchmarkDotNet()
- {
- BenchmarkDotNet.Reports.Summary? summary = BenchmarkRunner.Run<Tester>();
- }
- //public static void Run_with_manual_swopwatch()
- //{
- // Stopwatch sw = new();
- // Tester tester = new();
- // int nit = 50;
- // Call("Test_interface", tester.Test_interface, nit);
- // Call("Test_multiple_ifs", tester.Test_multiple_ifs, nit);
- // Call("Test_swith", tester.Test_swith, nit);
- // Call("Test_arr", tester.Test_arr, nit);
- // Call("Test_delegate_array", tester.Test_delegate_array, nit);
- //}
- //static void Call(string name, Func<double> method, int nit)
- //{
- // Stopwatch sw = new();
- // sw.Start();
- // double sum = 0;
- // for (int i = 0; i < nit; i++)
- // {
- // sum = method();
- // }
- // sw.Stop();
- // double dur = sw.Elapsed.TotalMilliseconds / nit;
- // Console.WriteLine($"{name}. Sum = {sum:N0}. Time per oper = {dur:N1} ms");
- //}
- const int m = 10_000;
- int[] random_indexes = new int[m];
- IInterface[] interface_implementations = new IInterface[m];
- double[] values = new double[m];
- Func<double>[] delegates = new Func<double>[m];
- public Tester()
- {
- for (int i = 0; i < m; i++)
- {
- random_indexes[i] = Random.Shared.Next(0, 10);
- interface_implementations[i] = IInterface.Get_random_implementation();
- values[i] = Get_value(Random.Shared.Next(0, 10));
- delegates[i] = Get_delegate(Random.Shared.Next(0, 10));
- }
- }
- public double Get_value(int random_index)
- {
- if (random_index == 0) return Methods.Fun1();
- else if (random_index == 1) return Methods.Fun2();
- else if (random_index == 2) return Methods.Fun3();
- else if (random_index == 3) return Methods.Fun4();
- else if (random_index == 4) return Methods.Fun5();
- else if (random_index == 5) return Methods.Fun6();
- else if (random_index == 6) return Methods.Fun7();
- else if (random_index == 7) return Methods.Fun8();
- else if (random_index == 8) return Methods.Fun9();
- else if (random_index == 9) return Methods.Fun10();
- else throw new Exception();
- }
- public Func<double> Get_delegate(int random_index)
- {
- if (random_index == 0) return Methods.Fun1;
- else if (random_index == 1) return Methods.Fun2;
- else if (random_index == 2) return Methods.Fun3;
- else if (random_index == 3) return Methods.Fun4;
- else if (random_index == 4) return Methods.Fun5;
- else if (random_index == 5) return Methods.Fun6;
- else if (random_index == 6) return Methods.Fun7;
- else if (random_index == 7) return Methods.Fun8;
- else if (random_index == 8) return Methods.Fun9;
- else if (random_index == 9) return Methods.Fun10;
- else throw new Exception();
- }
- [Benchmark]
- public double Test_interface()
- {
- double sum = 0;
- for (int i = 0; i < m; i++)
- {
- sum += interface_implementations[i].Get_value();
- }
- return sum;
- }
- [Benchmark]
- public double Test_multiple_ifs()
- {
- double sum = 0;
- for (int i = 0; i < m; i++)
- {
- int random_index = random_indexes[i];
- if (random_index == 0) sum += Methods.Fun1();
- else if (random_index == 1) sum += Methods.Fun2();
- else if (random_index == 2) sum += Methods.Fun3();
- else if (random_index == 3) sum += Methods.Fun4();
- else if (random_index == 4) sum += Methods.Fun5();
- else if (random_index == 5) sum += Methods.Fun6();
- else if (random_index == 6) sum += Methods.Fun7();
- else if (random_index == 7) sum += Methods.Fun8();
- else if (random_index == 8) sum += Methods.Fun9();
- else if (random_index == 9) sum += Methods.Fun10();
- else throw new Exception();
- }
- return sum;
- }
- [Benchmark]
- public double Test_swith()
- {
- double sum = 0;
- for (int i = 0; i < m; i++)
- {
- switch (random_indexes[i])
- {
- case 0: sum += Methods.Fun1(); break;
- case 1: sum += Methods.Fun2(); break;
- case 2: sum += Methods.Fun3(); break;
- case 3: sum += Methods.Fun4(); break;
- case 4: sum += Methods.Fun5(); break;
- case 5: sum += Methods.Fun6(); break;
- case 6: sum += Methods.Fun7(); break;
- case 7: sum += Methods.Fun8(); break;
- case 8: sum += Methods.Fun9(); break;
- case 9: sum += Methods.Fun10(); break;
- default: throw new Exception();
- }
- }
- return sum;
- }
- [Benchmark]
- public double Test_arr()
- {
- double sum = 0;
- for (int i = 0; i < m; i++)
- {
- sum += values[i];
- }
- return sum;
- }
- [Benchmark]
- public double Test_delegate_array()
- {
- double sum = 0;
- for (int i = 0; i < m; i++)
- {
- sum += delegates[i]();
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement