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

  • Home
  • SEARCH
  • 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 6247839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:56:55+00:00 2026-05-24T12:56:55+00:00

I assumed lambda functions , delegates and anonymous functions with the same body would

  • 0

I assumed lambda functions, delegates and anonymous functions with the same body would have the same “speed”, however, running the following simple program:

static void Main(string[] args)
{
    List<int> items = new List<int>();

    Random random = new Random();

    for (int i = 0; i < 10000000; i++)
    {
        items.Add(random.Next());
    }

    Stopwatch watch;
    IEnumerable<int> result;

    Func<int, bool> @delegate = delegate(int i)
    {
        return i < 500;
    };
    watch = Stopwatch.StartNew();
    result = items.Where(@delegate);
    watch.Stop();
    Console.WriteLine("Delegate: {0}", watch.Elapsed.TotalMilliseconds);

    Func<int, bool> lambda = i => i < 500;
    watch = Stopwatch.StartNew();
    result = items.Where(lambda);
    watch.Stop();
    Console.WriteLine("Lambda: {0}", watch.Elapsed.TotalMilliseconds);

    watch = Stopwatch.StartNew();
    result = items.Where(i => i < 500);
    watch.Stop();
    Console.WriteLine("Inline: {0}", watch.Elapsed.TotalMilliseconds);

    Console.ReadLine();
}

I get:

Delegate: 4.2948 ms

Lambda: 0.0019 ms

Anonymous: 0.0034 ms

Although negligible, why are these three – apparently identical – methods running at different speeds? What’s happening under the hood?


Update:

As suggested by the comments, the following “forces” the Where by calling ToList() on it. In addition, a loop is added to offer more run data:

while (true) 
{
    List<int> items = new List<int>();

    Random random = new Random();

    for (int i = 0; i < 10000000; i++)
    {
        items.Add(random.Next());
    }

    Stopwatch watch;
    IEnumerable<int> result;

    Func<int, bool> @delegate = delegate(int i)
    {
        return i < 500;
    };
    watch = Stopwatch.StartNew();
    result = items.Where(@delegate).ToList();
    watch.Stop();
    Console.WriteLine("Delegate: {0}", watch.Elapsed.TotalMilliseconds);

    Func<int, bool> lambda = i => i < 500;
    watch = Stopwatch.StartNew();
    result = items.Where(lambda).ToList();
    watch.Stop();
    Console.WriteLine("Lambda: {0}", watch.Elapsed.TotalMilliseconds);

    watch = Stopwatch.StartNew();
    result = items.Where(i => i < 500).ToList();
    watch.Stop();
    Console.WriteLine("Inline: {0}", watch.Elapsed.TotalMilliseconds);
    Console.WriteLine(new string('-', 12));

}

The above code results in ~120 ms for each function.

  • 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-24T12:56:57+00:00Added an answer on May 24, 2026 at 12:56 pm

    A lambda expression is an anonymous function. “Anonymous function” refers to either a lambda expression or an anonymous method (which is what you’ve called a “delegate” in your code).

    All three operations are using delegates. The second and third are both using lambda expressions. All three will execute in the same way, with the same performance characteristics.

    Note that there can be a difference in performance between:

    Func<int, int> func = x => ...;
    for (int i = 0; i < 10000; i++) {
        CallFunc(func);
    }
    

    and

    for (int i = 0; i < 10000; i++) {
        CallFunc(x => ...) // Same lambda as before
    }
    

    It depends on whether the compiler is able to cache the delegate created by the lambda expression. That will in turn depend on whether it captures variables etc.

    For example, consider this code:

    using System;
    using System.Diagnostics;
    
    class Test
    {
        const int Iterations = 1000000000;
    
        static void Main()
        {
            AllocateOnce();
            AllocateInLoop();
        }
    
        static void AllocateOnce()
        {
            int x = 10;
    
            Stopwatch sw = Stopwatch.StartNew();
            int sum = 0;
            Func<int, int> allocateOnce = y => y + x;
            for (int i = 0; i < Iterations; i++)
            {
                sum += Apply(i, allocateOnce);
            }
            sw.Stop();
            Console.WriteLine("Allocated once: {0}ms", sw.ElapsedMilliseconds);
        }
    
        static void AllocateInLoop()
        {
            int x = 10;
    
            Stopwatch sw = Stopwatch.StartNew();
            int sum = 0;
            for (int i = 0; i < Iterations; i++)
            {
                sum += Apply(i, y => y + x);
            }
            sw.Stop();
            Console.WriteLine("Allocated in loop: {0}ms", sw.ElapsedMilliseconds);
        }
    
        static int Apply(int loopCounter, Func<int, int> func)
        {
            return func(loopCounter);
        }
    }
    

    The compiler is smart, but there’s still a difference. Using Reflector, we can see that AllocateInLoop is effectively compiled to:

    private static void AllocateInLoop()
    {
        Func<int, int> func = null;
        int x = 10;
        Stopwatch stopwatch = Stopwatch.StartNew();
        int sum = 0;
        for (int i = 0; i < Iterations; i++)
        {
            if (func == null)
            {
                func = y => y + x;
            }
            sum += Apply(i, func);
        }
        stopwatch.Stop();
        Console.WriteLine("Allocated in loop: {0}ms", stopwatch.ElapsedMilliseconds);
    }
    

    So still only a single delegate instance is created, but there’s extra logic within the loop – an extra nullity test on each iteration, basically.

    On my machine that makes about a 15% difference in performance.

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

Sidebar

Related Questions

I assumed joinable would indicate this, however, it does not seem to be the
the following lambda statemement returns null, when i was hoping it would return a
I have recently upgraded my g++ so I can enjoy lambda functions. Everything is
If I use a lambda expression like the following // assume sch_id is a
I installed what I assumed would be the latest version ( link ) of
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I had a situation come up that required running a lambda expression on the
This a conceptual question on how one would implement the following in Lisp (assuming
I assumed when I have, say Project A and Project B, and Project B
I'm trying to parse standard simple types (in the sense of lambda calculus) using

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.