• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Method Performance

 
pamir sonmez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about making a super class, putting shared code in it and then extend that class. That would avoid duplicate code.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pamir sonmez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12342
39
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!