9

When, if ever, is it faster to pass arguments as arguments to a static method rather than have the method be non-static and access the same values via instance members. Assume the method accesses these members in a read-only fashion.

All other things being equal, calling a static method is slightly faster than calling an instance method.

All other things being equal, calling a method with no arguments is slightly faster than calling one with arguments.

Consider:

private Thing _thing; void DoTheThing() { _thing.DoIt(); } 

Versus this equivalent code:

private Thing _thing; // caller's responsibility to pass "_thing" static void DoTheThing(Thing thing) { thing.DoIt(); } 

I can't think of a real-world situation where this kind of optimisation would really add any value, but as a thought experiment (for those who like to discuss this kind of thing), is there really a benefit, and if so then how many arguments (of what types etc) tip the balance the other way?

Would any other factors play into the consideration of this? The static method accesses _thing as a local variable rather than a field, for example.

6
  • 2
    +1 Good question. When you run Code Analysis on your code in VS you get error CA1822 if your methods in your class can be marked as static. It always irks me, and I really wonder if there's any benefit. Commented Oct 15, 2009 at 16:53
  • @BFree -- if you follow the link I included in the question, you can see that calling a static method is ever-so-slightly faster. In IL, the caller doesn't have to push a reference to the target onto the stack (the JIT probably annuls this), and the EE doesn't have to check this target for nullability. Commented Oct 15, 2009 at 17:00
  • 2
    Yes, there is a performance gain, but you aren't likely to see it until you are calling the method in a loop for hundreds of thousands of times. Premature optimization. Commented Oct 15, 2009 at 17:03
  • It improves my comprehension performance if the first consideration to making a method static or not is if it conceptually belongs to the object type, not an instance of an object. For example if object dog has a static Bark() method. I am going to be looking for a comment with an explanation. Commented Oct 15, 2009 at 17:09
  • @Darren -- agreed. I ask this as more of an afternoon (for me, anyway) thought experiment for those who find the inner workings of the language/EE/JIT/type-system interesting. Commented Oct 15, 2009 at 17:19

3 Answers 3

6

There's one possible performance benefit I can thnk of (for a non-virtual method): the static method doesn't need to test a reference for nullity first (to throw a NullReferenceException where appropriate).

I don't think this currently gives any advantage, but it's a possible one. I'm not sure it would apply in your particular example, though - and it's hard to see how it would apply in any case where you actually wanted to use the value.

Sign up to request clarification or add additional context in comments.

Comments

0

In your case (I'm assuming the code sample would be in the Thing class) static and non-static will have no speed difference at all. This is from YOUR link:

  • 0.2 0.2 inlined static call
  • 0.2 0.2 inlined this inst call

So there is no sense at all in making it static for a speed boost.

Also take into account that the values provided in your linked page are from .Net 1.1 and way outdated.

8 Comments

An interesting point, though it only applies when methods are inlined. The example I used would probably be inlined but larger methods (or those with value types as arguments, IIRC) would not be inlined in which case the static call costs 6.1ns and the instance call costs 6.8ns.
According to the link you have the instance call would be 6.2ns instead of 6.1ns for the static. Also there are no numbers I would in fact assume that this would be overcompensated by the additional parameter which means that the static version is probably even slightly slower.
Just to clarify: It's a this instance call
@ Drew - Functions with value types as arguments get properly inlined as of .NET 3.5 SP1. @ Foxfire - Correct me if I'm wrong, but I think that an instance method has an implicit this reference passed to it as an argument, where it's explicit in the static method, but they both take 1 argument.
I guess it might be an instance call. Thanks for pointing out that distinction as a factor.
|
0

I am not sure about the performance statics among static methods and instance methods.

But I believe that the decision should be taken whether you make it as a static method or instance method on the basis of object design. If by calling your method, the state of the object is not altered, then you should make that method as static method (method for the type, not for a perticular instance of the type).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.