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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:18:48+00:00 2026-05-18T05:18:48+00:00

Why is a Func<> created from an Expression<Func<>> via .Compile() considerably slower than just

  • 0

Why is a Func<> created from an Expression<Func<>> via .Compile() considerably slower than just using a Func<> declared directly ?

I just changed from using a Func<IInterface, object> declared directly to one created from an Expression<Func<IInterface, object>> in an app i am working on and i noticed that the performance went down.

I have just done a little test, and the Func<> created from an Expression takes “almost” double the time of an Func<> declared directly.

On my machine the Direct Func<> takes about 7.5 seconds and the Expression<Func<>> takes about 12.6 seconds.

Here is the test code I used (running Net 4.0)

// Direct
Func<int, Foo> test1 = x => new Foo(x * 2);

int counter1 = 0;

Stopwatch s1 = new Stopwatch();
s1.Start();
for (int i = 0; i < 300000000; i++)
{
 counter1 += test1(i).Value;
}
s1.Stop();
var result1 = s1.Elapsed;



// Expression . Compile()
Expression<Func<int, Foo>> expression = x => new Foo(x * 2);
Func<int, Foo> test2 = expression.Compile();

int counter2 = 0;

Stopwatch s2 = new Stopwatch();
s2.Start();
for (int i = 0; i < 300000000; i++)
{
 counter2 += test2(i).Value;
}
s2.Stop();
var result2 = s2.Elapsed;



public class Foo
{
 public Foo(int i)
 {
  Value = i;
 }
 public int Value { get; set; }
}

How can i get the performance back ?

Is there anything i can do to get the Func<> created from the Expression<Func<>> to perform like one declared directly ?

  • 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-18T05:18:48+00:00Added an answer on May 18, 2026 at 5:18 am

    As others have mentioned, the overhead of calling a dynamic delegate is causing your slowdown. On my computer that overhead is about 12ns with my CPU at 3GHz. The way to get around that is to load the method from a compiled assembly, like this:

    var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
                 new AssemblyName("assembly"), AssemblyBuilderAccess.Run);
    var mod = ab.DefineDynamicModule("module");
    var tb = mod.DefineType("type", TypeAttributes.Public);
    var mb = tb.DefineMethod(
                 "test3", MethodAttributes.Public | MethodAttributes.Static);
    expression.CompileToMethod(mb);
    var t = tb.CreateType();
    var test3 = (Func<int, Foo>)Delegate.CreateDelegate(
                    typeof(Func<int, Foo>), t.GetMethod("test3"));
    
    int counter3 = 0;
    Stopwatch s3 = new Stopwatch();
    s3.Start();
    for (int i = 0; i < 300000000; i++)
    {
        counter3 += test3(i).Value;
    }
    s3.Stop();
    var result3 = s3.Elapsed;
    

    When I add the above code, result3 is always just a fraction of a second higher than result1, for about a 1ns overhead.

    So why even bother with a compiled lambda (test2) when you can have a faster delegate (test3)? Because creating the dynamic assembly is much more overhead in general, and only saves you 10-20ns on each invocation.

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

Sidebar

Related Questions

Func<a, out b, bool> , just don't compile, how to declare that i want
I'm using the Dynamic.ParseLambda method from the Dynamic LINQ library to create expressions, compile
When I use Expression.Lambda( ... ).Compile() in order to create a delegate from an
My goal is to create a list from menu.bin. This is the func: pitem
I created two modules, one with def _func(): print hi and another def func():
I want to create a dynamic Expression<Func<T,Y>> . Here is the code which works
(See below solution I created using the answer I accepted) I'm trying to improve
Im using Moq to create mocks of a data set. I have created a
I just implemented FluentValidation , everything seems awesome except one awkward little detail. @using
I have an Expression that is used to get a list of items from

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.