aspose file tools
The moose likes Performance and the fly likes Static Method Performance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Static Method Performance" Watch "Static Method Performance" New topic
Author

Static Method Performance

pamir sonmez
Ranch Hand

Joined: May 31, 2010
Posts: 46
I have many class, all use the same method like getFormula(String[] array1,String[] array2)
And Every class has the same implemantation itself.

If I create another common class and I define this method as static method so every class will call it from this class and
there wont be any code duplicate, is this effect performance? Because I need an efficient programme also?

If static method is not the correct way, what is the correct solution?

Thanks
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
How about making a super class, putting shared code in it and then extend that class. That would avoid duplicate code.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
This is a design question not a performance question. Performance will be fine either way. If your method requires not state (i.e. all the values it needs are passed into the method) then it can be a static method.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
pamir sonmez
Ranch Hand

Joined: May 31, 2010
Posts: 46
then you say there is no performance difference between static method usage and extending from super class.
That is what I want to learn exactly, static method does not have poor performance.If it had poor performance, I can use another design issue more efficient like extending it.

thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

you shouldn't design for performance. You should design for what makes sense and is simplest to read. Most of the time, any performance gain/loss is going to be negligible compared to time spent debugging/refactoring code.

It's best to design code that is easy to read and understand. THEN, if you have a performance issue, consider how to improve things by using a profiler to determine where the slowdown really is. Odds are it won't be in your static method, but somewhere else.


Never ascribe to malice that which can be adequately explained by stupidity.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
I suspect one of the other may be a minor amount faster, but it is academic as both are blisteringly fast. Always focus on the best design, and monitor your code for any performance issues that may pop up.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
A couple other points. Often a setting like "-server", or a garbage collection setting would have a far bigger effect on performance than this and would effect more of your program than this simple method. Also, anything we say may be different depending on what OS you use, and what version of the JDK you use.

I did however perform a performance test and didn't see a substantive difference. And to show why it is purely an academic question I could invoke the method over 500,000,000 times in a second! That is a lot of calls per second. For more than the typical application would have to do. If the methods did any work whatsoever they would always be more of a bottleneck than the invocation time.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Static Method Performance
 
Similar Threads
class methods vs instance method
difference between class with static method and singleton pattern
Nested synchronized for raf
Method call as an object reference
Static Methods with local variables.. Thread Safe?