File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Static methods could cause performance issues? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Static methods could cause performance issues?" Watch "Static methods could cause performance issues?" New topic
Author

Static methods could cause performance issues?

Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
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?


Thank you.<br /> <br />- Jess Sie<br /> <a href="mailto:jess@see-consulting.com" rel="nofollow">jess@see-consulting.com</a>
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3878
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


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Jes Sie
Ranch Hand

Joined: Jul 24, 2001
Posts: 188
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
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
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
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1825
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

Joined: Jul 28, 2000
Posts: 1825
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
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
I am using JDK 1.40
Mike Brock
Greenhorn

Joined: Dec 30, 2002
Posts: 15
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
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.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
IntelliJ Java IDE
 
subject: Static methods could cause performance issues?
 
Threads others viewed
Static or non-static method?
Singleton's static method(s) & the need for getInstance()
Static classes and functions
using static methods
Related to Singleton class
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture