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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:41:37+00:00 2026-06-08T19:41:37+00:00

I have a simple scenario, where i am trying to test the performance of

  • 0

I have a simple scenario, where i am trying to test the performance of a expression compiled tree on an list of stock objects. Below is the code

The performance of expression compiled tree is 5x slower than static lambda call. I am not sure whether this is a standard performance one can expect with expression compiled tree. Would appreciate any insight.

LambdaExpression();
List<Stock> stocks = new List<Stock>();
for (int ctr = 0; ctr <= 5000000; ctr++)
{
    Stock stk1 = new Stock() { Price = ctr, Symbol = "A", CloseDate = DateTime.Now, FaceValue = ctr } ;
    stocks.Add(stk1);
}
CompileTimeLamda(a);
DynamicLambda(a);


public static void LambdaExpression()
{
    ParameterExpression CS1 = Expression.Parameter(typeof(Stock), "d");

    var line1 = Expression.Equal(Expression.Property(CS1, typeof(Stock).GetProperty("Symbol")), Expression.Constant("MSFT", typeof(string)));
    var line2 = Expression.GreaterThan(Expression.Property(Expression.Property(CS1, typeof(Stock).GetProperty("CloseDate")),typeof(DateTime).GetProperty("Millisecond")), 
                                 Expression.Constant(0, typeof(int)));
    var line3 = Expression.GreaterThan(Expression.Property(CS1, typeof(Stock).GetProperty("Price")), Expression.Constant((double)0, typeof(double)));
    var line4 = Expression.And(line1,line2);
    var line5 = Expression.OrElse(line4, line3);

    func = Expression.Lambda<Func<Stock, bool>>(line5, new ParameterExpression[] {  CS1 } ).Compile();
}


public static void DynamicLambda(List<Stock> stks)
{
    Stopwatch watch = new Stopwatch();
    watch.Start();
    foreach (var d in stks)
    {
        func(d);
    }
    watch.Stop();
    Console.WriteLine("Dynamic Lambda :" + watch.ElapsedMilliseconds);
}

public static void CompileTimeLamda(List<Stock> stks)
{
    Stopwatch watch = new Stopwatch();
    watch.Start();
    foreach (var d in stks)
    {
        if (d.Symbol == "MSFT" && d.CloseDate.Millisecond > 0 ||
                                  (d.Price) > 0) ;
    }
    watch.Stop();
    Console.WriteLine("Compile Time Lamda " +watch.ElapsedMilliseconds);
}
  • 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-08T19:41:39+00:00Added an answer on June 8, 2026 at 7:41 pm

    The difference has to do with the compiler having more information and spending more effort on optimizing the code if you compile it at compile time, rather than at runtime… Also, using a lambda, you have a more “flexible” program (you can choose the lambda at run time). That comes at the cost of
    an extra function call, and losing a lot of potential optimizations.

    To make a more “fair” comparison, you can compare a static lambda vs a dynamic lambda using something like:

    Func<Stock, bool> compileTime = (Stock d) => (d.Symbol == "MSFT" && d.CloseDate.Millisecond > 0) || d.Price > 0;
    

    instead of the hardcoded code..

    There you’ll also find a difference, but a slightly smaller one… The difference is for the same reason (more optimization)… You can decrease the difference by optimizing your lambda by hand (although that isn’t always possible, since the compiler can create valid CLI code that can’t be created manually with a lambda).

    But for example, if you change your dynamic lambda from:

    var line5 = Expression.OrElse(line4, line3);
    

    to:

    var line5 = Expression.OrElse(line3, line4);
    

    You’ll see how the lambda performs between 1x and 2x of your original compiled code.

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

Sidebar

Related Questions

Say I have a simple client/server scenario with one method: // client code $client
I am trying to have a simple block of JS code call my Silverlight
I am trying to do a performance test on an asp.net website. I have
I have what seems to be a fairly simple scenario that I am trying
I am trying to implement a simple scenario where I have a git repo
I have a 'simple' scenario: Read some JSON file, Filter or change some of
I have a simple scenario where i want to output only json to the
I have a very simple scenario but poor experience in this area and googling
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
I have a very simple scenario, using NHibernate: one abstract base class animal; two

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.