fork download
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. public class Test
  5. {
  6. private const int TEST_COUNT = 10000000;
  7.  
  8. private static double[] xs;
  9.  
  10. private static double[] ys;
  11.  
  12. private static double a;
  13.  
  14. private static double l;
  15.  
  16. private static double x;
  17.  
  18. private static double y;
  19.  
  20. private static void Reset()
  21. {
  22. for (var i = 0; i < TEST_COUNT; i++)
  23. {
  24. xs[i] = 100;
  25. ys[i] = 100;
  26. }
  27. }
  28.  
  29. public static void Main(string[] args)
  30. {
  31. var sw = new Stopwatch();
  32.  
  33. xs = new double[TEST_COUNT];
  34. ys = new double[TEST_COUNT];
  35.  
  36. // test 1
  37. Reset();
  38. sw.Reset();
  39. sw.Start();
  40. for (var i = 0; i < TEST_COUNT; i++)
  41. {
  42. x = xs[i];
  43. y = ys[i];
  44. a = Math.Atan2(y, x);
  45. l = Math.Sqrt(x * x + y * y);
  46. xs[i] = (l - 10) * Math.Cos(a);
  47. ys[i] = (l - 10) * Math.Sin(a);
  48. }
  49. var test1 = sw.Elapsed;
  50.  
  51. // test 2
  52. Reset();
  53. sw.Reset();
  54. sw.Start();
  55. for (int i = 0; i < TEST_COUNT; i++)
  56. {
  57. x = xs[i];
  58. y = ys[i];
  59. l = Math.Sqrt(x * x + y * y);
  60. xs[i] = (l - 10) * (x / l);
  61. ys[i] = (l - 10) * (y / l);
  62. }
  63. var test2 = sw.Elapsed;
  64.  
  65.  
  66. Console.WriteLine("test1: " + (test1.TotalMilliseconds / TEST_COUNT * 1000000).ToString("0.00") + "ns/op");
  67. Console.WriteLine("test2: " + (test2.TotalMilliseconds / TEST_COUNT * 1000000).ToString("0.00") + "ns/op");
  68. Console.ReadKey(true);
  69. }
  70. }
Success #stdin #stdout 2.97s 24016KB
stdin
Standard input is empty
stdout
test1: 237.51ns/op
test2: 32.93ns/op