views:

332

answers:

6

What is the performance concern with static method over non-static methods? I have read that Static methods are better in terms of performance but i want to know, how they are faster? If a method is not using any instance member then our compiler should take care of it and treat it as static method.

+3  A: 

See this question.

Here's the excerpt:

a static call is 4 to 5 times faster than constructing an instance every time you call an instance method. However, we're still only talking about tens of nanoseconds per call

Ngu Soon Hui
Note that this is comparing to constructing a new instance for each call; it does not compare the more usual case of constructing an instance once, for several calls.
John Saunders
If you are *just* referring to answers from another question, why not just post the link as a comment, or vote as a duplicate?
Marc Gravell
Also note that that article was back in 2003. The CLR has changed quite a bit since then.
Jon Skeet
+1  A: 

I doubt the compiler will treat it as a static method, although you can check for yourself. The benefit would be no creation of the instance. No garbage collector to worry about. And only the static constructor to be called, if there is one.

Yuriy Faktorovich
C++ compiler does that, why not .Net(C#) compiler
viky
+7  A: 

Edit: Eric comments more on this here, and hints that there are some times when call is used... although note that his new() example isn't guaranteed ;-p


In the original compiler (pre-1.1), the compiler did treat non-virtual instance methods (without this) as static; the problem was that this lead to some odd problems with null checking, i.e.

obj.SomeMethod();

didn't threw an exception (for obj=null and non-virtual method SomeMethod which didn't touch this). Which was bad if you ever changed the implementation of SomeMethod. When they investigated the cost of adding the explicit null check (i.e. null-check then static-call), it turned out to be just the same as using a virtual-call, so they did that instead, which makes it far more flexible and predictable.

Note that the "don't throw an exception" is also entirely the behaviour if SomeMethod is an extension-method (static).

I think at one point you could emit IL to invoke a regular instance method via static-call, but the last time I tried I got the "oh no you don't!" message from the CLR (this operation may destabilise the runtime); either they blocked this entirely, or (perhaps more likely) I borked the custom IL.

Marc Gravell
I have read that C++ compiler does this, so why didn't they make C# compiler that much smart?
viky
It isn't about smart... like I explained, there wasn't any noticeable difference between doing a "null-check and static call" vs "virtual call" (which has a null-check built in). So why have two different options to maintain?
Marc Gravell
A: 

static methods fast,because constructing an instance

buy if you only create a instance and save static member , performance is equal

they are very small in total performance

so .......

Clover
+4  A: 

Yes a static call would be faster - you don't need to create an instance of the object before you call the method. (Although you obviously won't notice the difference)

In practical terms it doesn't matter if the compiler optimizes a method (makes the instance method static) - you won't call the instance method unless you've already created the instance already, right?

At the end of the day you should rather try to optimize your code for maintainability rather than trying to save 3 nanoseconds here or there.

Jaco Pretorius
i wish our C# compiler to be smart enough to do this optimization just like C++ compiler. Thanks for explaining !!
viky
A: 

yes static method is fast but the memory acquired by the static variable is not controlled by GC and is not released even if it is not needed, so that is an issue.

but more than anything else you should consider the design of the allpication as the memory and speed has increased by days but your design may suck if you dont make use of static variables properly.

Vinay Pandey
You don't need to have a static variable to call a static method. They're different concepts.
George V. Reilly