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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:11:45+00:00 2026-06-07T21:11:45+00:00

I read this post and I tried to do the same but I came

  • 0

I read this post and I tried to do the same but I came across a very strange behavior when comparing the del.DynamicINvoke(args) Vs. del(args)

Update

So after Jon and usr comments i post now new working code.

I’ll really appreciate any help!

Code:

using System;
using System.Diagnostic;
using System.Threading;

namespace DynamicInvokeVsInvoke {
  public class Program {
    static void Main(string[] args) {
      var objArgs = new object[] {100, 1.2345678 };
      Action<int, double> a = (i, d) => {};
      Action<object[]> action = o => a((int)o[0], (double)o[1]);
      var sw = new Stopwatch();
      sw.Start();
      for (int i = 0; i < 1000; i++)
         a.DynamicInvoke(objArgs);
      Console.WriteLine("Dynamic: " + sw.ElapsedMilliseconds);
      sw.Stop();
      sw = new Stopwatch();
      sw.Start();
      for (int i = 0; i < 1000; i++)
         action(objArgs);
      Console.WriteLine("Invoke: " + sw.ElapsedMilliseconds);
      sw.Stop();
    }
  }
}

Results:

When ‘a’ is empty method and the loop runs 1000000 times the DynamicInvoke took approximately 4000 ms and the direct Invoke took 20 ms

When i’m put in a Thread.Sleep(20) and the loop runs 1000 times then the DynamicInvoke and the direct Invoke took approximately 20 seconds

p.s. i can’t copy/paste from vs for some reason so i write the code manually if you see syntax error please let me know

  • 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-07T21:11:48+00:00Added an answer on June 7, 2026 at 9:11 pm

    First thing to note: when looking at performance always ensure you are running a release/optimized build, and run it without the IDE attached. You can usually get away with ctrl+f5 in Visual Studio, but if in doubt: run it from the command line.

    When ‘a’ is empty method and the loop runs 1000000 times the DynamicInvoke took approximately 4000 ms and the direct Invoke took 20 ms

    Fair enough; I get 2729ms for DynamicInvoke and 8ms for direct invoke; but that is what we expect; we know that DynamicInvoke is slower. But everything here is as we expect. In fact, if we correct for the fact that Invoke is unfairly running an extra level of delegate invoke (which we should expect to roughly-double the time taken), we get:

    static void Main()
    {
        int x = 100;
        double y = 1.2345678;
        var objArgs = new object[] { x, y };
        Action<int, double> a = (i, d) => { };
        var sw = new Stopwatch();
        sw.Start();
        const int loop = 1000000;
        for (int i = 0; i < loop; i++)
            a.DynamicInvoke(objArgs);
        Console.WriteLine("Dynamic: " + sw.ElapsedMilliseconds);
        sw.Stop();
        sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < loop; i++)
            a(x,y);
        Console.WriteLine("Invoke: " + sw.ElapsedMilliseconds);
        sw.Stop();
    }
    

    with results:

    Dynamic: 2785
    Invoke: 3
    

    So far, nothing unexpected.

    When i’m put in a Thread.Sleep(20) and the loop runs 1000 times then the DynamicInvoke and the direct Invoke took approximately 20 seconds

    Yep, 20ms x 1000 is definitely 20 seconds. The 20ms sleep time is not exact; we expect some variance around that number, which adds up when multiplied by 1000. In particular, the sleep interrupt isn’t going to fire for every single CPU cycle, so we should probably expect the slightly irregular sleep cycle to eat up the difference between a Invoke and DynamicInvoke (about 2.75 microseconds per call).

    Changing loop to 1000 and adding a Thread.Sleep(20) I get:

    Dynamic: 20395
    Invoke: 20370
    

    Again, nothing surprising here. In fact, with the slight randomness introduced by a Thread.Sleep(), multiplied by 1000 I would find it perfectly acceptable if Invoke occasionally took longer than Dynamic.

    I see nothing in the least bit surprising in the numbers.

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

Sidebar

Related Questions

I read this post but I can't get it working: Change Background Color... I
Scenario Having already read a post on this on the same site, which didn't
I read that great post on Visual Studio 2008 annoyances, but didn't see this
I was reading about buffer, stack and heap overflows. I read this post as
On this post , I read about the usage of XMPP. Is this sort
I read this comment in the OpenID post on the stackoverflow blog. Kibbee says
I read at a few places (in the doc and in this blog post
This question is related to the previous post. How to save file and read
I read this post last night and it got me thinking. I like python
I've researched and tried numerous options to try and get this to work, but

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.