Common Intermediate Language и системное программирование в Microsoft.Net. Макаров А.В - 162 стр.

UptoLike

double h = (b-a)/n, sum = 0.0;
for (int i = 0; i < n; i++)
sum += h*f.Eval((i+0.5)*h);
return sum;
}
static void Main()
{
int num = 10000000;
double a = 0.0;
double b = 10.0;
string s = “2*x*x*x+3*x*x+4*x+5”;
Parser parser = new Parser(s);
Expression expr = parser.Parse();
DateTime t1 = DateTime.Now;
Function f1 = new InterpretingFunction(expr);
double s1 = Integrate(f1,a,b,num);
DateTime t2 = DateTime.Now;
Function f2 = CompileToCS(expr);
DateTime t2_2 = DateTime.Now;
double s2 = Integrate(f2,a,b,num);
DateTime t3 = DateTime.Now;
Function f3 = CompileToCIL(expr);
DateTime t3_2 = DateTime.Now;
double s3 = Integrate(f3,a,b,num);
DateTime t4 = DateTime.Now;
Console.WriteLine(“Interpreter: “+s1+” (“+(t2-t1)+”)”);
Console.WriteLine(“C#: “+s2+” (“+
(t2_2-t2)+” + “+(t3-t2_2)+”)”);
Console.WriteLine(“CIL: “+s3+” (“+
(t3_2-t3)+” + “+(t4-t3_2)+”)”);
}
}
Исходный код программы Integral
311
assembly.DefineDynamicModule(“f.dll”, “f.dll”);
TypeBuilder typeBuilder =
module.DefineType(
“FunctionCIL”,
TypeAttributes.Public | TypeAttributes.Class,
typeof(Function)
);
ConstructorBuilder cons =
typeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
new Type[] { }
);
ILGenerator consIl = cons.GetILGenerator();
consIl.Emit(OpCodes.Ldarg_0);
consIl.Emit(OpCodes.Call,typeof(object).GetConstructor(new
Type[0]));
consIl.Emit(OpCodes.Ret);
MethodBuilder evalMethod =
typeBuilder.DefineMethod(
“Eval”,
MethodAttributes.Public | MethodAttributes.Virtual,
typeof(double),
new Type[] { typeof(double) }
);
ILGenerator il = evalMethod.GetILGenerator();
expr.GenerateCIL(il);
il.Emit(OpCodes.Ret);
Type type = typeBuilder.CreateType();
ConstructorInfo ctor = type.GetConstructor(new Type[0]);
return ctor.Invoke(null) as Function;
}
static double Integrate(Function f, double a, double b, int n)
{
310
CIL и системное программирование в Microsoft .NET
310                         CIL и системное программирование в Microsoft .NET   Исходный код программы Integral                                        311


            assembly.DefineDynamicModule(“f.dll”, “f.dll”);                                  double h = (b-a)/n, sum = 0.0;

          TypeBuilder typeBuilder =                                                          for (int i = 0; i < n; i++)
            module.DefineType(                                                                 sum += h*f.Eval((i+0.5)*h);
               “FunctionCIL”,
               TypeAttributes.Public | TypeAttributes.Class,                                 return sum;
               typeof(Function)                                                          }
            );
                                                                                         static void Main()
          ConstructorBuilder cons =                                                      {
            typeBuilder.DefineConstructor(                                                 int num = 10000000;
               MethodAttributes.Public,                                                    double a = 0.0;
               CallingConventions.Standard,                                                double b = 10.0;
               new Type[] { }                                                              string s = “2*x*x*x+3*x*x+4*x+5”;
            );
                                                                                             Parser parser = new Parser(s);
          ILGenerator consIl = cons.GetILGenerator();                                        Expression expr = parser.Parse();
          consIl.Emit(OpCodes.Ldarg_0);
          consIl.Emit(OpCodes.Call,typeof(object).GetConstructor(new                         DateTime t1 = DateTime.Now;
                                 Type[0]));                                                  Function f1 = new InterpretingFunction(expr);
          consIl.Emit(OpCodes.Ret);                                                          double s1 = Integrate(f1,a,b,num);

          MethodBuilder evalMethod =                                                         DateTime t2 = DateTime.Now;
            typeBuilder.DefineMethod(                                                        Function f2 = CompileToCS(expr);
               “Eval”,                                                                       DateTime t2_2 = DateTime.Now;
               MethodAttributes.Public | MethodAttributes.Virtual,                           double s2 = Integrate(f2,a,b,num);
               typeof(double),
               new Type[] { typeof(double) }                                                 DateTime t3 = DateTime.Now;
            );                                                                               Function f3 = CompileToCIL(expr);
                                                                                             DateTime t3_2 = DateTime.Now;
          ILGenerator il = evalMethod.GetILGenerator();                                      double s3 = Integrate(f3,a,b,num);
          expr.GenerateCIL(il);
          il.Emit(OpCodes.Ret);                                                              DateTime t4 = DateTime.Now;

          Type type = typeBuilder.CreateType();                                              Console.WriteLine(“Interpreter: “+s1+” (“+(t2-t1)+”)”);
                                                                                             Console.WriteLine(“C#:     “+s2+” (“+
          ConstructorInfo ctor = type.GetConstructor(new Type[0]);                             (t2_2-t2)+” + “+(t3-t2_2)+”)”);
          return ctor.Invoke(null) as Function;                                              Console.WriteLine(“CIL:     “+s3+” (“+
      }                                                                                        (t3_2-t3)+” + “+(t4-t3_2)+”)”);
                                                                                         }
      static double Integrate(Function f, double a, double b, int n)                 }
      {