Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static methods more efficient?

In terms of memory and time, is it better to make a method static?

like image 881
Yaron Naveh Avatar asked Jan 26 '10 19:01

Yaron Naveh


People also ask

Why is static method better?

They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.

Are static methods good or bad?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous.

Are static methods faster Java?

As expected, virtual method calls are the slowest, non-virtual method calls are faster, and static method calls are even faster.

Is static method faster than instance method?

Static methods are faster but less OOP. If you'll be using design patterns, static method is likely bad code.


2 Answers

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."

like image 80
Hans Passant Avatar answered Sep 21 '22 17:09

Hans Passant


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.

like image 29
LBushkin Avatar answered Sep 22 '22 17:09

LBushkin