Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:29:42+00:00 2026-06-17T09:29:42+00:00

I was trying to compare dynamic compilation with standard compilcation. And dynamic compilation seems

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:29:43+00:00Added an answer on June 17, 2026 at 9:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to compare and print only members with Inactive status. The problem is that
i was trying to compare the difference between 2 dates, but it seems the
Im trying to compare Calendar s with JPA2. The query looks somewhat like that:
Does the NSFileManager method contentsEqualAtPath:andPath: create a dynamic checksum to compare two files, does
When trying to compare software versions 5.12 to 5.8, version 5.12 is newer, however
Im trying to compare these two chars but on win 32 Visual Studio 2008:
I am trying to compare a date against the first of next month from
I am trying to compare a list of forbidden folders read from a file.
I am trying to compare two tools execution time, which I have installed in
I am trying to compare values on data frame rows, and removing all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.