• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static vs Instance

 
Murat Balkan
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Suppose I have a method that makes a complex computation. Do you advice to mark this method as static looking from the performance perspective? I think this has to bring some perf. because of the disabling of inheritance..
Am I wrong?
Thanks,
Murat
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the important point is whether the method dispatch can be completely determined at compile time, or whether it has to be left until run time to decide. Compile time ought to be quicker, although the HotSpot may sometimes be able greatly to reduce the difference.

The dispatch of static methods is certainly determined at compile time. However, this sometimes applies to instance methods. For instance, if the method is private, or the class is final, or the method is final.

If the algorithm of the method does not need to know the instance, it is probably a good idea to make the method static. However, some OO purists say that static methods are evidence of failed OO design...
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the static - instance difference would be almost impossible to measure.
I have seen suggestions that supposedly making instance methods final allows the JIT compiler to in-line them, thus doing away with any method call delays. If you manage to actually measure any differences, let us know.
Bill
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In almost any case, it is save to assume that method calls don't take any time - *especially* if the method itself is complex!
 
Stephen Bloch
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate on what Ilja said... if there is any performance difference between a static method and a non-static one, it'll be in the process of calling the method. Once you're into the method, both should execute identically, so if the method is doing a lot of work, the time it takes to call the method becomes utterly insignificant.

The only situation in which it might make a difference is when
  • the method body is extremely fast (like a method that just increments a variable),
  • the method is called millions of times, and
  • for some reason the method can't be inlined by the compiler. (I don't know what it takes for a method to be inline-able....)

  •  
    Mark Lybarger
    Ranch Hand
    Posts: 72
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i am interested in how static methods show a failed OO design?
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Mark Lybarger:
    i am interested in how static methods show a failed OO design?


    OO gives us two tools for managing dependencies in our code:

    - putting operations and the data they operate on at one entity (a class/object), because they typically need to be changed together, and

    - using polymorphic calls to operations, so that implementations of objects can be exchanged (even at runtime) without their clients caring about it.

    In Java, static methods definitely fail the second test, often also the first (as they tend to work on method parameters). Therefore heavy use of static methods is an indicator for an overly procedural (non-OO) design.

    Does that help?
     
    steve souza
    Ranch Hand
    Posts: 862
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've seen this question asked many times in performance forums: "Which is faster static or instance methods?"

    Software developers are too cautious in their answer here. The answer is: "This choice will NEVER-EVER-EVER will make a noticable difference in application performance. Also, NEVER-EVER-EVER let performance be the deciding point for whether you use static or instance methods."

    The time it takes to call either type of method is so fast that it can safely be ignored. I have an open source performance measuring tool called JAMon. JAMon works much likes a stopwatch - with a start and stop method. You can disable jamon at runtime. If JAMon is disabled I can call the start and stop instance methods 30,000,000 times each in 1 second!!! That is 60,000,000 method calls a second, on my crappy 4 year old pc!
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've experienced a few situations where there was a noticable difference - using static methods made the program run faster. It's extremely uncommon, true, but since you say "NEVER-EVER-EVER" I can't agree. Sometimes, it matters. Stephen describes the conditions for this quite well.
     
    steve souza
    Ranch Hand
    Posts: 862
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Truthfully, I don't even agree with "NEVER-EVER-EVER" and I wrote it, but the poor code that results from developers that use statics over instance methods will probably not make a noticeable performance difference and so is is a much worse sin than my 99.9% true statement.

    If I say that static vs instance method calls make a difference most people will remember that headline and forget the fact that in most programs it makes no noticeable difference. I think the biggest problem we have to overcome in the performance world is premature optimization, and questions like static vs instance performance stink of someone that is prematurely optimizing.

    I have been business apps for 20 years, and I have NEVER-EVER-EVER seen a case where static vs instance method calls made a noticeable difference in performance to end users. I wonder has any reader of this forum ever seen a case where the performance difference made a noticeable difference in program performance.
    [ December 20, 2004: Message edited by: steve souza ]
     
    Mitch Robertson
    Greenhorn
    Posts: 1
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Static methods do significantly increase performance on repetitive methods done millions of times.

    For example in my application I'm writing, I use a static method on a helper class to warp a Bezier curve. It is a small method (3 lines), but if it weren't static (or final) i couldn't inline it with the -0 option on the javac compiler. If it weren't inlined, it is an extra byte-code instruction to go to the location in memory where that method exists. I think private methods are only inlined if using the -0 option on the javac compiler.

    One thing I might add is that if a performance isn't the requirement then nice coding style is ALWAYS the best option. There is nothing worse than awful ugly code that has no need to be optimized to that level but was - just because some guy is trying to show off his overly complicated coding style.

    I am trying to transform potentially millions of these curves a second for a rendering engine I'm writing so every little spec counts!

    Mitch
    [ March 19, 2008: Message edited by: Mitch Robertson ]
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for an interesting reply.

    Do note, however, that you replied to a thread from 2004. In general, on this site, we have a rule DontWakeTheZombies.
     
    Jiangwen Wei
    Greenhorn
    Posts: 2
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    if you use static method instead of instance method only to gain more performance, I suggest you find another way to achieve it. because you may gain higher performance by doing this, but this must be very very very little.
     
    Bruce Jin
    Ranch Hand
    Posts: 672
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OO gives us two tools for managing dependencies in our code:

    - putting operations and the data they operate on at one entity (a class/object), because they typically need to be changed together, and

    - using polymorphic calls to operations, so that implementations of objects can be exchanged (even at runtime) without their clients caring about it.



    I use static call a lot. Maybe this is because I did a few years of procedural programming before coding Java.
    How do I change the habit of doing static call and use instance?
    Thanks.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic