Are static methods more efficient?

Solution 1:

Usually yes, there is no need to pass the "this" reference. That reference is passed in the ECX register so there is no additional stack space required. The register is already set if you make the call from an instance method of the same class, there will be no savings at all. But it can help relieving the pressure on a x86 CPU core when the method is in another class, x86 doesn't have a lot of registers. Seeing a measurable perf improvement would be exceedingly rare.

I do religiously mark methods of a class that don't use instance members as static. I value the inherent contract provided by the static keyword: "this method does not mutate the object state."

Solution 2:

You should make a method static if it does not require any state information from the class that it is part of.

If you don't care about polymorphism, you can write any method as either instance or static by just deciding whether to take class instance members and pass them to the method as arguments. What you should consider, is whether the syntax is natural, whether the code is easy to understand and meaningful, and so on.

You probably shouldn't worry about optimizing at this level, because the performance overhead of a instance vs. static method is negligible. Yes, there's some space used in the dispatch table for the type (if the method is virtual) - but it's a tiny, constant overhead. Yes, there's also a tiny overhead in invoking an instance method versus a static method - but again it's tiny.

This seems like a level of micro-optimization, that unless you have measurable, tangible evidence to believe is actually affecting program performance, you should avoid. In fact, if you do things wrong, the cost of passing in additional parameters (copying them onto the stack, etc) rather than accessing them through the hidden this reference of your type, may result in worse performance.

You are better of analyzing the semantics of the method, and making the static/instance decision on that basis.

Solution 3:

If you are going to pass in the instance anyway (SomeStaticMethod(obj, "abc", 123);), then not really. You could only usefully use a static method in a scenario without polymorphism, and in such cases it is highly likely that any simple things like properties would have been inlined anyway.

Use objects "naturally" (obj.SomeMethod("abc",123);) - keep the code simple, and profile to find performance issues - it is very unlikely to be in the difference between instance and static unless you are running some very tight loops. There are some scenarios where it might matter, but they are pretty specialised.

Solution 4:

There is little to no difference between a static method and a non-virtual instance method. The latter just has the this prointer/reference as a "hidden" argument. In the generated machine code, both kinds of calls look very similar.

A method should be static if it does not depend on/modify the object.

Virtual methods (overridable) on the other hand require the caller to look up the exact implementation in the so called vtable. In addition to preventing the compiler from inlining very small methods (simple property accessors are inlined very often) the lookup takes a couple of cycles.

Still, virtual methods are the fastest kind of dynamic dispatch available in C#/on the CLR. Much faster than delegates and reflection.