• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static methods could cause performance issues?  RSS feed

 
Jes Sie
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jes Sie
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small test

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
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JDK 1.40
 
Mike Brock
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!