Hi, Suppose I have a method that makes a complex computation. Do you advice to mark this method as static looking from the performance perspective? I think this has to bring some perf. because of the disabling of inheritance.. Am I wrong? Thanks, Murat
I believe that the important point is whether the method dispatch can be completely determined at compile time, or whether it has to be left until run time to decide. Compile time ought to be quicker, although the HotSpot may sometimes be able greatly to reduce the difference.
The dispatch of static methods is certainly determined at compile time. However, this sometimes applies to instance methods. For instance, if the method is private, or the class is final, or the method is final.
If the algorithm of the method does not need to know the instance, it is probably a good idea to make the method static. However, some OO purists say that static methods are evidence of failed OO design...
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I suspect the static - instance difference would be almost impossible to measure. I have seen suggestions that supposedly making instance methods final allows the JIT compiler to in-line them, thus doing away with any method call delays. If you manage to actually measure any differences, let us know. Bill
In almost any case, it is save to assume that method calls don't take any time - *especially* if the method itself is complex!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
To elaborate on what Ilja said... if there is any performance difference between a static method and a non-static one, it'll be in the process of calling the method. Once you're into the method, both should execute identically, so if the method is doing a lot of work, the time it takes to call the method becomes utterly insignificant.
The only situation in which it might make a difference is when
the method body is extremely fast (like a method that just increments a variable),
the method is called millions of times, and
for some reason the method can't be inlined by the compiler. (I don't know what it takes for a method to be inline-able....)
i am interested in how static methods show a failed OO design?
Joined: Jul 11, 2001
Originally posted by Mark Lybarger: i am interested in how static methods show a failed OO design?
OO gives us two tools for managing dependencies in our code:
- putting operations and the data they operate on at one entity (a class/object), because they typically need to be changed together, and
- using polymorphic calls to operations, so that implementations of objects can be exchanged (even at runtime) without their clients caring about it.
In Java, static methods definitely fail the second test, often also the first (as they tend to work on method parameters). Therefore heavy use of static methods is an indicator for an overly procedural (non-OO) design.
I've seen this question asked many times in performance forums: "Which is faster static or instance methods?"
Software developers are too cautious in their answer here. The answer is: "This choice will NEVER-EVER-EVER will make a noticable difference in application performance. Also, NEVER-EVER-EVER let performance be the deciding point for whether you use static or instance methods."
The time it takes to call either type of method is so fast that it can safely be ignored. I have an open source performance measuring tool called JAMon. JAMon works much likes a stopwatch - with a start and stop method. You can disable jamon at runtime. If JAMon is disabled I can call the start and stop instance methods 30,000,000 times each in 1 second!!! That is 60,000,000 method calls a second, on my crappy 4 year old pc!
I've experienced a few situations where there was a noticable difference - using static methods made the program run faster. It's extremely uncommon, true, but since you say "NEVER-EVER-EVER" I can't agree. Sometimes, it matters. Stephen describes the conditions for this quite well.
"I'm not back." - Bill Harding, Twister
Joined: Jun 26, 2002
Truthfully, I don't even agree with "NEVER-EVER-EVER" and I wrote it, but the poor code that results from developers that use statics over instance methods will probably not make a noticeable performance difference and so is is a much worse sin than my 99.9% true statement.
If I say that static vs instance method calls make a difference most people will remember that headline and forget the fact that in most programs it makes no noticeable difference. I think the biggest problem we have to overcome in the performance world is premature optimization, and questions like static vs instance performance stink of someone that is prematurely optimizing.
I have been business apps for 20 years, and I have NEVER-EVER-EVER seen a case where static vs instance method calls made a noticeable difference in performance to end users. I wonder has any reader of this forum ever seen a case where the performance difference made a noticeable difference in program performance. [ December 20, 2004: Message edited by: steve souza ]
Static methods do significantly increase performance on repetitive methods done millions of times.
For example in my application I'm writing, I use a static method on a helper class to warp a Bezier curve. It is a small method (3 lines), but if it weren't static (or final) i couldn't inline it with the -0 option on the javac compiler. If it weren't inlined, it is an extra byte-code instruction to go to the location in memory where that method exists. I think private methods are only inlined if using the -0 option on the javac compiler.
One thing I might add is that if a performance isn't the requirement then nice coding style is ALWAYS the best option. There is nothing worse than awful ugly code that has no need to be optimized to that level but was - just because some guy is trying to show off his overly complicated coding style.
I am trying to transform potentially millions of these curves a second for a rendering engine I'm writing so every little spec counts!
if you use static method instead of instance method only to gain more performance, I suggest you find another way to achieve it. because you may gain higher performance by doing this, but this must be very very very little.