Static Vs Instance method performance differences

I think it is too late to post this info. Still I can see some question related to this topic in public newsgroups, hence this entry.

Here is my simple class.

      public class MyClass

      {

            public static void StaticHello()

            {

                  Console.WriteLine(”Hello”);

            }

            public void InstanceHello()

            {

                  Console.WriteLine(”Hello”);

            }

}

Here is a snippet from main method.

//Staic Method Invokation

MyClass.StaticHello();

 

      //Instance method invokation

      MyClass myClass = new MyClass();

      myClass.InstanceHello();

Both methods will do the same. Now question. What about the performance? Which one should I use? Well, all depends on what you want J

If you look the IL code for this app,

 .locals init ([0] class StaticMethodTest.MyClass myClass)

  IL_0000:  call       void StaticMethodTest.MyClass::StaticHello()

  IL_0005:  newobj     instance void StaticMethodTest.MyClass::.ctor()

  IL_000a:  stloc.0

  IL_000b:  ldloc.0

  IL_000c:  callvirt   instance void StaticMethodTest.MyClass::InstanceHello()

  IL_0011:  ret

ie static method is using calll and instance method is using callvirt instruction. Even if you edit your IL and replace  callvirt with call, application will work. But, callvirt will do null checking of the object, before it try to invoke methods. Obviously, static methods will be faster than instance method, since there is no null checking. Again, the difference is too small. 

Ireland – One more Euro country - visit

Yes. I have reached here in Dundalk, Ireland last Sunday (27th Feb).  I thought of writing one blog entry.

We (Prasad, Srijit & Myself) started from Bangalore (Bangalore – Mumbai – Amsterdam – Dublin) on Saturday night. We took taxi from Dublin (Ireland capital) to Dundalk, almost 1-hour drive and reached by 2PM. Nice apartment was arranged near to Dundalk Shopping Center. Most of the shops were closed, since it was Sunday. But TESCO, Europe’s most famous retail shop, was located in shopping center. We got TESCO timings from apartment owner, 24 hours on weekdays and weekends from 10 to 8. Now time to eat some Indian stuff (may be first and last Indian food in Ireland), I had taken MTR read-to-eat meals from Bangalore. Apartment has all facilities except Internet Access. Our neighbors too don’t have a wireless router, checked each corner of apartment; otherwise they could have enable it for an internet buff. We got a rental car for next 2 months, hence planning to visit near by places on coming weekends. Hope I can update some snaps later.

Oh, forgot to mention some general info about Duldalk(located about half way between Dublin and Belfast). Temperature: –3 to 6 degree C, Currency: Euro, Time: GMT, Country: Ireland Republic