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 7626405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:19:15+00:00 2026-05-31T05:19:15+00:00

if a and b are double, does anyone know if ((dynamic)a) + ((dynamic) b)

  • 0

if a and b are double, does anyone know if

((dynamic)a) + ((dynamic) b) 

is faster or slower than

Func<double,double,double>((x,y) => x + y)(a, b)

And can you explain why?

  • 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-05-31T05:19:15+00:00Added an answer on May 31, 2026 at 5:19 am

    The first version will always be slower. In very simple terms with this version everything that the compiler can do at compile time has now to be done at runtime, ie. checking the types of a and b, finding if they support the + operator and so on. It will cache a lot of this stuff, but still it is much more work than invoking a delegate.

    In the second version all those checks can be done during compilation. The cost at runtime are only the creation and invocation of the delegate.

    For example, consider those methods:

    static T Add<T>(T a, T b)
    {
      return ((dynamic)a) + ((dynamic)b);
    }
    

    in contrast to

    static T Add<T>(T a, T b, Func<T, T, T> adder)
    {
      return adder(a, b);
    }
    

    This is what the compiler generates from the first method:

    private static T Add<T>(T a, T b)
    {
      if (Program.<Add>o__SiteContainer0<T>.<>p__Site1 == null)
      {
        Program.<Add>o__SiteContainer0<T>.<>p__Site1 = CallSite<Func<CallSite, object, T>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(T), typeof(Program)));
      }
      Func<CallSite, object, T> arg_98_0 = Program.<Add>o__SiteContainer0<T>.<>p__Site1.Target;
      CallSite arg_98_1 = Program.<Add>o__SiteContainer0<T>.<>p__Site1;
      if (Program.<Add>o__SiteContainer0<T>.<>p__Site2 == null)
      {
        Program.<Add>o__SiteContainer0<T>.<>p__Site2 = CallSite<Func<CallSite, object, object, object>>.Create(Binder.BinaryOperation(CSharpBinderFlags.None, ExpressionType.Add, typeof(Program), new CSharpArgumentInfo[]
        {
          CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), 
          CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
        }));
      }
      return arg_98_0(arg_98_1, Program.<Add>o__SiteContainer0<T>.<>p__Site2.Target(Program.<Add>o__SiteContainer0<T>.<>p__Site2, a, b));
    }
    

    I’ve done some crude measurements and on my machine the first version is about 5 times slower than the second. I have to admit that I would have expected the difference to be larger.

    Update: As for a prove why this is slower than a delegate: Given that the generated call site code also involves the invocation of a delegate (arg_98_0), this piece of code (invocation of delegate + X) must necessarily be slower than using a delegate only.

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

Sidebar

Related Questions

Does anyone know where I can find that algorithm? It takes a double and
Does anyone know how to find out the precision of long double on a
Does anyone know how I can open an attachment from a work item without
Does anyone know of a double valued gauge in wxPython? I would like to
How can I calculate the logarithm of a BigDecimal? Does anyone know of any
Does anyone know how I can get my results currently in qry IQueryable object
Does anyone know whether a double is implicitly cast to double? ( Nullable type)
Does anyone know how can I replace this 2 symbol below from the string
Does anyone know what i am doing wrong and explain to me why it
Does anyone know where code can be found for Metaphone 3 matching for T-SQL

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.