// File RTCG5.cs --- evaluation of polynomials // sestoft@itu.dk * 2002 // Mobile Intel P3: The specialized code is faster even for // polynomials of degree 5, and even though the function must be // called through a delegate. // AMD Athlon (1st generation): The specialized code is faster only // for degree 12 and higher. using System; using System.Reflection; using System.Reflection.Emit; class RTCG5 { public static void Main(String [] args) { int order = args.Length-1; double[] cs = new double[order]; for (int i=0; i=0; i--) res = res * x + cs[i]; return res; } public static void EvalGen(ILGenerator ilg, double[] cs) { // x is arg_0 ilg.Emit(OpCodes.Ldc_R8, 0.0); // push res on stack for (int i=cs.Length-1; i>=0; i--) { ilg.Emit(OpCodes.Ldarg_0); // load x ilg.Emit(OpCodes.Mul); // compute res * x if (cs[i] != 0.0) { ilg.Emit(OpCodes.Ldc_R8, cs[i]); // load cs[i] ilg.Emit(OpCodes.Add); // compute x * res + cs[i] } } ilg.Emit(OpCodes.Ret); // return res; } public delegate double D2D(double x); } // Crude timing utility ---------------------------------------- public class Timer { private DateTime start; public Timer() { start = DateTime.Now; } public double Check() { TimeSpan dur = DateTime.Now - start; return dur.TotalSeconds; } }