using System; using System.Diagnostics; namespace fastexponent { class Program { static double FastExp(double x) { var tmp = (long)(1512775 * x + 1072632447); return BitConverter.Int64BitsToDouble(tmp << 32); } static void Main(string[] args) { double[] x = new double[1000000]; double[] ex = new double[x.Length]; double[] fx = new double[x.Length]; Random r = new Random(); for (int i = 0; i < x.Length; ++i) x[i] = r.NextDouble() * 40; Stopwatch sw = new Stopwatch(); sw.Start(); for (int j = 0; j < x.Length; ++j) ex[j] = Math.Exp(x[j]); sw.Stop(); double builtin = sw.Elapsed.TotalMilliseconds; sw.Reset(); sw.Start(); for (int k = 0; k < x.Length; ++k) fx[k] = FastExp(x[k]); sw.Stop(); double custom = sw.Elapsed.TotalMilliseconds; double min = 1, max = 1; for (int m = 0; m < x.Length; ++m) { double ratio = fx[m] / ex[m]; if (min > ratio) min = ratio; if (max < ratio) max = ratio; } Console.WriteLine("minimum ratio = " + min.ToString() + ", maximum ratio = " + max.ToString() + ", speedup = " + (builtin / custom).ToString()); } } }