fork(1) download
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace fastexponent
  5. {
  6. class Program
  7. {
  8. static double FastExp(double x)
  9. {
  10. var tmp = (long)(1512775 * x + 1072632447);
  11. return BitConverter.Int64BitsToDouble(tmp << 32);
  12. }
  13.  
  14. static void Main(string[] args)
  15. {
  16. double[] x = new double[1000000];
  17. double[] ex = new double[x.Length];
  18. double[] fx = new double[x.Length];
  19. Random r = new Random();
  20. for (int i = 0; i < x.Length; ++i)
  21. x[i] = r.NextDouble() * 40;
  22.  
  23. Stopwatch sw = new Stopwatch();
  24. sw.Start();
  25. for (int j = 0; j < x.Length; ++j)
  26. ex[j] = Math.Exp(x[j]);
  27. sw.Stop();
  28. double builtin = sw.Elapsed.TotalMilliseconds;
  29. sw.Reset();
  30. sw.Start();
  31. for (int k = 0; k < x.Length; ++k)
  32. fx[k] = FastExp(x[k]);
  33. sw.Stop();
  34. double custom = sw.Elapsed.TotalMilliseconds;
  35.  
  36. double min = 1, max = 1;
  37. for (int m = 0; m < x.Length; ++m) {
  38. double ratio = fx[m] / ex[m];
  39. if (min > ratio) min = ratio;
  40. if (max < ratio) max = ratio;
  41. }
  42.  
  43. Console.WriteLine("minimum ratio = " + min.ToString() + ", maximum ratio = " + max.ToString() + ", speedup = " + (builtin / custom).ToString());
  44. }
  45. }
  46. }
  47.  
Success #stdin #stdout 0.4s 79936KB
stdin
Standard input is empty
stdout
minimum ratio = 0.960597678477954, maximum ratio = 1.01965903274147, speedup = 0.295571114757978