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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:49:45+00:00 2026-05-23T04:49:45+00:00

I found this strange behaviour in .NET and even after looking into CLR via

  • 0

I found this strange behaviour in .NET and even after looking into CLR via C# again I am still confused. Let’s assume we have an interface with one method and a class that imlements it:

interface IFoo
{
    void Do();
}

class TheFoo : IFoo
{
    public void Do()
    {
        //do nothing
    }
}

Then we want just to instantiate this class and call this Do() method a lot of times in two ways: using concrete class variable and using an interface variable:

TheFoo foo1 = new TheFoo();

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (long i = 0; i < 1000000000; i++)
    foo1.Do();
stopwatch.Stop();
Console.Out.WriteLine("Elapsed time: " + stopwatch.ElapsedMilliseconds);

IFoo foo2 = foo1;

stopwatch = new Stopwatch();
stopwatch.Start();
for (long i = 0; i < 1000000000; i++)
    foo2.Do();
stopwatch.Stop();
Console.Out.WriteLine("Elapsed time: " + stopwatch.ElapsedMilliseconds);

Surprisingly (at least to me) the elapsed times are about 10% different:

Elapsed time: 6005
Elapsed time: 6667

The difference is not that much, so I would not worry a lot about this in most cases. However I just can’t figure out why this happens even after looking in IL code, so I would appreciate if somebody point me to something obvious that I am missing.

  • 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-23T04:49:45+00:00Added an answer on May 23, 2026 at 4:49 am

    You have to look at the machine code to see what is going on. When you do, you’ll see that the jitter optimizer has completely removed the call to foo1.Do(). Small methods like that get inlined by the optimizer. Since the body of the method contains no code, no machine code is generated at all. It cannot make the same optimization on the interface call, it is not quite smart enough to reverse-engineer that the interface method pointer actually points to an empty method.

    Check this answer for a list of the common optimizations performed by the jitter. Note the warnings about profiling mentioned in that answer.

    NOTE: looking at the machine code in release build requires changing an option. By default the optimizer is disabled when you debug code, even in the release build. Tools + Options, Debugging, General, untick “Suppress JIT optimization on module load”.

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

Sidebar

Related Questions

Found this rather strange bug in IE8; element.style.top is limited to 1342177 pixels. Even
Strange program hang, what does this mean in debug? After attaching windbg I found
We found strange behaviour in DropDownListFor (ASP.NET MVC3 release). It selects ViewBag property value
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
I've come across a strange behaviour in .NET/Reflection and cannot find any solution/explanation for
Good day, everyone. I found strange behavior within ASP.NET engine when it handles non-existent
It seems that I get the strange behaviour of a GIF-Animation starting over again,
I found a strange behaviour in an ant build of a git repository. Here's
I'm trying to visualize Mandelbrot set with OpenGL and have found very strange behaviour
I've found some strange behaviour of firefox 3.6 - when I call HtmlPage.Window.Confirm() method

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.