Hi guys, Sorry, this is definitely a dumb question despite my many years of Java experience. If I've got a class which has static methods, will the static methods be a cause to performance degradation if many users are calling that method? How does the JVM do this? If a first client uses the method, any client/thread would just use it without waiting for the first caller to finish, right?
There is no performance degradation. Methods of any sort (static or instance) really just come down to bytecodes that are executed within a thread by the JVM. There's no waiting involved unless a method is synchronized. Kyle
Originally posted by Kyle Brown: There is no performance degradation. Methods of any sort (static or instance) really just come down to bytecodes that are executed within a thread by the JVM. There's no waiting involved unless a method is synchronized. Kyle
Thanks. That's a fair comment. Now, what happens in a EJB environment (whereby programmatically spawning my own threads is not encouraged)....should I take that as a single threaded env or do I take it as multi threaded env (as I believe EJB container spawns threads in the background?)
Tom Angioletti
Greenhorn
Joined: Jun 25, 2001
Posts: 10
posted
0
I agree with the above and would just like to note that static method calls are actually a bit faster than non-static method calls.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Tom Angioletti: I agree with the above and would just like to note that static method calls are actually a bit faster than non-static method calls.
Well, such statements are a little bit problematic, as performance of method calls *heavily* depends on the implementation of the JVM and the underlying hardware. For most applications it would be most appropriate to think of method call performance as simply irrelevant, IMO.
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
As suggested earlier the performance optimisation is very very low considering the number of iterations that I have considered. So I think there is no reason to Focus here for performance.
Rahul Mahindrakar
Ranch Hand
Joined: Jul 28, 2000
Posts: 1825
posted
0
Oh by the way I have windows 2000 and the output of the program is total time 420 total time 241
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Rahul Mahindrakar: Oh by the way I have windows 2000 and the output of the program is total time 420 total time 241
What JDK?
Rahul Mahindrakar
Ranch Hand
Joined: Jul 28, 2000
Posts: 1825
posted
0
I am using JDK 1.40
Mike Brock
Greenhorn
Joined: Dec 30, 2002
Posts: 15
posted
0
It is worth noting, that static methods can improve performance, especially in later-version compilers from Sun and even Jikes. Take the following code for example:
Not that this is a practical example, but what happens here, is the compiler may see an opportunity to 'inline' the code. In doing this, the compiler will actually will forgo the static method call all-together and merge the operations in append() directly into the test() method. This is of course not always possible, depending on method's security scope, etc. As a general rule, you should use static methods when the operation within the method does not need to access class members, hence a 'static' operation. Mike.
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 851
posted
0
Based on the above example: Total time to execute a nonstatic method 100,000,000 times = 420 milliseconds Total time to execute a static method 100,000,000 times = 241 milliseconds The question, "what is faster a static or nonstatic method?" is asked a fair ammount. I have never worked on a program where the differences sited above would have made any difference to the end user. Most apps I write (business apps) are IO bound (network or database). The choice of using static or non-static should always be based on design considerations not performance considerations.