Advertisement
Guest User

Untitled

a guest
Apr 14th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. private const double TWO_PI = Math.PI * 2;
  2.         private const int CYCLES = 10000000;
  3.         private const int SEED = 873407789;
  4.         private static double[] RANDOM_NUMBERS;
  5.  
  6.         static void Main(string[] args)
  7.         {
  8.             //precompile methods of the assembly for better performance
  9.             Tools.PreJitCompile.Methods(System.Reflection.Assembly.GetExecutingAssembly());
  10.  
  11.             //feeding probe array
  12.             var r = new Random(SEED);
  13.             RANDOM_NUMBERS = new double[CYCLES];
  14.             for (var i = 0L; i < CYCLES; i++)
  15.             {
  16.                 RANDOM_NUMBERS[i] = r.NextDouble() * TWO_PI;
  17.             }
  18.             //probe system trig
  19.             BenchSystem(out var systemTime);
  20.             //prepare for usage, one time effort
  21.             UltimateTrigonometry.InitializeTrigonometricTables();
  22.             //probe ultimate trig
  23.             BenchUltimateTrig(out var ultimateTime);
  24.  
  25.             Console.WriteLine("___________________________");
  26.             Console.WriteLine("Method  |  Time");
  27.             Console.WriteLine("___________________________\r\n");
  28.  
  29.             Console.WriteLine("System  |  {0}", systemTime);
  30.  
  31.             Console.WriteLine("Ultimate|  {0}", ultimateTime);
  32.  
  33.             Console.WriteLine("\r\nSystem {0}% faster", (int)(ultimateTime / systemTime * 100d) - 100);
  34.  
  35.             Console.Read();
  36.         }
  37.  
  38.         private static void BenchSystem(out TimeSpan deltaT)
  39.         {
  40.             var sw = new System.Diagnostics.Stopwatch();
  41.             sw.Start();
  42.             for(var i = 0; i < CYCLES; i++)
  43.             {
  44.                 Math.Sin(RANDOM_NUMBERS[i]);
  45.             }
  46.             sw.Stop();
  47.             deltaT = sw.Elapsed;
  48.         }
  49.  
  50.         private static void BenchUltimateTrig(out TimeSpan deltaT)
  51.         {
  52.             var sw = new System.Diagnostics.Stopwatch();
  53.             sw.Start();
  54.             for (var i = 0; i < CYCLES; i++)
  55.             {
  56.                 UltimateTrigonometry.Sine(RANDOM_NUMBERS[i]);
  57.             }
  58.             sw.Stop();
  59.             deltaT = sw.Elapsed;
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement