Domanda

I was trying to compare dynamic compilation with standard compilcation. And dynamic compilation seems to be a lot slower. 1400% slower according to my benchmark.

Here is the method that generates a delegate that calls a dynamically compiled code:

public static Function Eval(string sCSCode)
{
    CSharpCodeProvider c = new CSharpCodeProvider();
    ICodeCompiler icc = c.CreateCompiler();
    CompilerParameters cp = new CompilerParameters();

    cp.ReferencedAssemblies.Add("system.dll");

    cp.CompilerOptions = "/optimize";
    //cp.GenerateInMemory = true;
    StringBuilder sb = new StringBuilder("");

    sb.Append("using System;\n");

    sb.Append("namespace CSCodeEvaler{ \n");
    sb.Append("public class CSCodeEvaler{ \n");
    sb.Append("public double sin(double x) { return Math.Sin(x); }");
    sb.Append("public double cos(double x) { return Math.Cos(x); }");
    sb.Append("public double sqrt(double x) { return Math.Sqrt(x); }");
    sb.Append("public const double PI = Math.PI;");
    sb.Append("public double Calculate(double x, double y){\n");
    sb.Append("return " + sCSCode + "; \n");
    sb.Append("} \n");
    sb.Append("} \n");
    sb.Append("}\n");

    CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
    if (cr.Errors.Count > 0)
    {
        throw new Exception("ERROR: " + cr.Errors[0].ErrorText);
        return null;
    }

    System.Reflection.Assembly a = cr.CompiledAssembly;
    object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
    Type t = o.GetType();
    MethodInfo mi = t.GetMethod("Calculate");

    return delegate(double x, double y)
    {
        object[] oParams = new object[2];
        oParams[0] = x;
        oParams[1] = y;
        object s = mi.Invoke(o, oParams);
        return (double)s;
    };
}

Here's the code I created to test its performance:

private double f1(double x, double y)
{
    return Math.Sin(x) + Math.Cos(y);
}

private void button1_Click(object sender, EventArgs e)
{
    double sum = 0;
    long t1 = DateTime.Now.Ticks;                
    for (double x = 0; x <= 500; x += 0.1)
    {
        for (double y = 0; y <= 500; y += 0.1)
        {
            sum += f1(x, y);
        }
    }
    t1 = DateTime.Now.Ticks - t1;

    sum = 0;
    var f2 = FunctionConstructor.Eval("Math.Sin(x) + Math.Cos(y)");
    long t2 = DateTime.Now.Ticks;            
    for (double x = 0; x <= 500; x += 0.1)
    {
        for (double y = 0; y <= 500; y += 0.1)
        {
            sum += f2(x, y);
        }
    }
    t2 = DateTime.Now.Ticks - t2;
    long dt = t2 - t1;
    double q = dt / (double)t1;
    MessageBox.Show("WITHOUT dynamic compilation: " + t1 + "\n" + "WITH dynamic compilation: " + t2 + "\n" + "Calculation without dynamic compilation was " + dt + " ticks (" + (q-1)*100 + "%) slower");
}

According to my test, dynamic compilation is MUCH slower. Can this be solved somehow? Perhaps the problem is in the way I'm benchmarking?

È stato utile?

Soluzione

You are calling a method that takes very little time. Which makes the overhead in calling the method a major contributing factor in the speed of the code. Both the dynamic invoke and inability to inline the method are the time killers. You can see the difference by moving the inner loop inside the Calculate method. For example:

    sb.Append("public double Calculate(double x, double y){\n");
    sb.Append("double sum = 0; for (; y <= 500; y += 0.1) {\n");
    sb.Append("sum += " + sCSCode + ";\n");
    sb.Append("} return sum;\n");

and

    long t2 = DateTime.Now.Ticks;
    for (double x = 0; x <= 500; x += 0.1) {
        sum += f2(x, 0);
    }
    t2 = DateTime.Now.Ticks - t2;

And you'll see the difference disappear. There is no simple fix for this. The problem is not exactly that dynamically invoking code is slow, it is that allowing the jitter optimizer to generate optimal machine code makes it so incredibly fast. Which of course is a feature, not a bug.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top