Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can making a method static improve performance, and under what circumstances?

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.

like image 989
Drew Noakes Avatar asked Oct 15 '09 16:10

Drew Noakes


People also ask

What is the advantage of making a method static?

A static method belongs to the class rather than the object of a class. A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.

What happens if we make a method static?

"static" , which declares the method as one that belongs to the entire class and not a part of any objects of the class. The main must always be declared as static since the interpreter uses this method before any objects are created. if a method's functionality is for class/program level then we declare it as static.

Is it good practice to make methods static?

Static Methods/Variables are bad practice. In short: Yes. There are many disadvantages and static methods should almost never be used. Static methods allow procedural/functional code to be shoe-horned into an Object Oriented world.

What is static method and when it should be used?

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.


1 Answers

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.

like image 79
Jon Skeet Avatar answered Sep 27 '22 19:09

Jon Skeet