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

The Archive Base Latest Questions

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

I wrote two simple applications: 1. one using anonymous methods, and 2. one using

  • 0

I wrote two simple applications:
1. one using anonymous methods, and
2. one using simple methods.

Each of the methods above are doing a simple action:

int add(int n1, int n2) {return n1+n2}

And I call a simple for loop that will call the add method 10,000 times. The implementation using anonymous methods takes much less time than the other. Why? Is it because the JITter inlines anonymous methods?

delegate int ADD( int i1, int i2 );

private  void button1_Click( object sender, EventArgs e )
    {
        Stopwatch watch = new Stopwatch();
        watch.Reset();

        watch.Start();

        for( int i = 0 ; i < 10000 ; i++ )
        {
            add( i, i );
        }

        watch.Stop();
        Console.WriteLine("Normal Call " + watch.ElapsedTicks);

        watch.Reset();
        watch.Start();
        for( int i = 0 ; i < 10000 ; i++ )
        {
            ADD p = delegate( int n1, int n2 )
            {
                return n1 + n2;
            };

            p.Invoke( i, i );
        }

        watch.Stop();
        Console.WriteLine( watch.ElapsedTicks );

        Console.ReadLine();


    } 

    int add(int n1, int n2)
    {
        return n1 + n2;
    }
 }

The result – on release mode – compile on x64 ( pressed on the action button couple of times ):

Normal 1525
1275
Normal 480
477
Normal 371
370
Normal 372
371
Normal 477
479
Normal 477
477
Normal 564
702
Normal 478
476
  • 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-30T12:55:54+00:00Added an answer on May 30, 2026 at 12:55 pm

    I could not confirm your results.

    In my testing, the “normal” function performs about order of magnitude better than the delegate. Here is your slightly modified code that I used for testing:

    class Program {
    
        delegate int ADD(int i1, int i2);
    
        static int add(int n1, int n2) {
            return n1 + n2;
        }
    
        static void Main(string[] args) {
    
            Stopwatch watch = new Stopwatch();
    
            watch.Start();
    
            const int count = 100000;
    
            for (int i = 0; i < count; i++) {
                add(i, i);
            }
    
            watch.Stop();
            Console.WriteLine(watch.ElapsedTicks);
    
            ADD p = delegate(int n1, int n2) {
                return n1 + n2;
            };
    
            watch.Reset();
            watch.Start();
            for (int i = 0; i < count; i++) {
                p.Invoke(i, i);
            }
    
            watch.Stop();
            Console.WriteLine(watch.ElapsedTicks);
    
        }
    
    }
    

    This prints:

    103
    1321
    

    You must be doing something that skews the results.

    — EDIT —

    OK I’ve done another round of testing, this time with a WinForms application.

    • When WinForms application is compiled as ‘x86’, the results are the same as the console application above (function is faster than delegate).
    • When WinForms is compiled as ‘x64’, the function slows-down dramatically, roughly to the level of the delegate.
    • When function is made static, it speeds-up again (in ‘x64’ WinForms build).
    • The console application above never slows-down, even in ‘x64’ build and even when function is made non-static.

    Everything was measured outside debugger in Release configuration.

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

Sidebar

Related Questions

I'm using System V Shared Memory to communicate between my two PHP applications. One
I wrote two methods with a void type parameter: procedure Method1(const MyVar; size: cardinal);
I wrote two methods in class Util: public static final <T> T[] copy1(T[] source)
I wrote two methods to check there performance public class Test1 { private String
I am developing a simple android application using eclipse. I wrote a JUnit TestCase
I have a simple application which spawns two threads and assigns one with a
I'm trying to write a simple WPF app that has two ellipses, joined by
I had a task to write simple game simulating two players picking up 1-3
I had a task to write simple game simulating two players picking up 1-3
I'm trying to write a simple server in c that plays a two player

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.