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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:14:06+00:00 2026-05-30T14:14:06+00:00

Why is a Id == 999999 faster than a lambda expression driven comparison using

  • 0

Why is a Id == 999999 faster than a lambda expression driven comparison using a Predicate?

Maybe my test itself is not 100% the same, but this is just a sample to show something about a general question: Is a predicate slower than a common Id == 999999 ?

The predicate takes 150 ms while the common compare has 125 ms. From where comes the

difference/overhead? You might ask why I care for 25 ms. Well… I use a open type predicate also in a hierarchical find method and there the gap is much larger.

So I guess the lambda (creating for each “u” a delegate) + predicate is the problem? If not what is wrong with my setup?

   public class UtilitiesTest
        {
            [Test]
            public void Go()
            {
                var units = GetUnits();
                DateTime d = DateTime.Now;         
                var item = units.testme<MyUnit>(u => u.Id == 999999);

                TimeSpan t = DateTime.Now - d;

                Debug.WriteLine(t.TotalMilliseconds + " ms");


                var units1 = GetUnits();
                DateTime d1 = DateTime.Now;  

                MyUnit item1 = null;
                foreach (MyUnit unit in units1)
                {
                    if (unit.Id == 999999)
                    {
                        item1 = unit;
                        break;
                    }
                }
                TimeSpan t1 = DateTime.Now - d1;
                Debug.WriteLine(t1.TotalMilliseconds + " ms");
            }

            private IEnumerable<MyUnit> GetUnits()
            {
                for (int i = 0; i < 1000000; i++)          
                    yield return new MyUnit() { Id = i };            
            }        
        }

        class MyUnit
        {
            public int Id { get; set; }
        }

    public static T testme<T>(this IEnumerable<T> source, Predicate<T> condition) where T : class
            {
                foreach (T item in source)
                {
                    if (condition(item))
                    {
                        return item;
                    }
                }
                return default(T);
            }
  • 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-30T14:14:08+00:00Added an answer on May 30, 2026 at 2:14 pm

    I think there is a small difference in speed, but my first point would be that there are a few things I’d improve on in your test. When constructing these micro benchmark tests, it is important to always observe a few rules:

    1. When timing, use a Stopwatch instead of DateTime – it has a higher resolution and is more precise.
    2. Always make sure you warm up all code under test once before running it. Otherwise the first bit of code will have a tendency to run slower as it bears the majority cost of JIT’ing
    3. Always run the test multiple times (you have done this in your example).

    If I rework your test, I end up with something like this:

    public void Go()
    {
        // warmup
        Test_Equality();
        Test_Lambda();
    
        // timed tests
        Console.WriteLine(Test_Equality() + " ms");
        Console.WriteLine(Test_Lambda() + " ms");
    }
    
    public long Test_Lambda()
    {
        var units1 = GetUnits();
        var stopWatch1 = new Stopwatch();
        stopWatch1.Start();
        MyUnit item1 = units1.testme<MyUnit>(u => u.Id == 999999);
        return stopWatch1.ElapsedMilliseconds;
    }
    
    public long Test_Equality()
    {
        var units2 = GetUnits();
        var stopWatch2 = new Stopwatch();
        stopWatch2.Start();
        MyUnit item2;
        foreach (MyUnit unit in units2)
        {
            if (unit.Id == 999999)
            {
                item2 = unit;
                break;
            }
        }
    
        return stopWatch2.ElapsedMilliseconds;
    }
    

    I run this, I get figures that are roughly this:

    Test_Lambda: 68 ms 
    Test_Equality: 53 ms
    

    Overall though, I would expect there to be a small performance hit when calling the delegate/lambda version over the native call, in the same way that there is a small performance hit to calling a method via a delegate rather than calling the method directly. Behind the scenes, the compiler is generating extra code to support these lambda version of your test.

    Ultimately it is generating something along the lines of this:

    public class PossibleLambdaImpl
    {
        public bool Comparison(MyUnit myUnit)
        {
            return myUnit.Id == 9999999;
        }
    }
    

    Therefore, your lambda test is actually calling a method on a compiler generated class every time it evaluates.

    In fact – when I change your equality test to instead create the above PossibleLambdaImpl class once, and call PossibleLambdaImpl.Comparison each time round the loop, I get almost identical results to the lambda case:

    public long Test_PossibleLambdaImpl()
    {
        var units2 = GetUnits();
        var stopWatch2 = new Stopwatch();
        stopWatch2.Start();
        MyUnit item2;
        var possibleLambdaImpl = new PossibleLambdaImpl();
        foreach (MyUnit unit in units2)
        {
            if (possibleLambdaImpl.Comparison(unit))
            {
                item2 = unit;
                break;
            }
        }
        return stopWatch2.ElapsedMilliseconds;
    }
    

    [Note: there are others on this site who know far more about this than me – but roughly speaking I believe this is correct]

    In any case, the thing to remember is that this performance difference is tiny. Micro-benchmarks like this always accentuate the difference. There may be a 10%-20% performance difference between them according to your test, but if your real-life code only spends 0.001% of its time making this sort of call (for example) then this amounts to an absolutely tiny difference in executing code.

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

Sidebar

Related Questions

I'm using following jQuery function to change the opacity of text (it's color: #999999)
Below is the code i am using Again: $rand = mt_rand(100000,999999); $sql = select
I wrote a small program using a custom indexOf function but wanted to dismiss
Wondering if anyone knows if either of these methods would produce an output faster:
Given a string like #fff443 or #999999 How do I verify that the string
I want to create auto-hide menu, just the same thing like http://www.ringvemedia.com/shanghai-photos but the
I am using cakePHP 1.26. I was trying to update a Table using these
I understand that: head (map (2**) [1..999999]) Will only actually evaluate 2**1, and none
i wanted to create a field , which is all in same length. i.e
I'm lead dev for Bitfighter , and we're using Lua as a scripting language

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.