• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Static methods could cause performance issues?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?)
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using JDK 1.40
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic