wood burning stoves 2.0*
The moose likes Performance and the fly likes Static vs Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Static vs Instance" Watch "Static vs Instance" New topic
Author

Static vs Instance

Murat Balkan
Ranch Hand

Joined: Sep 10, 2002
Posts: 127
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

Joined: Oct 30, 2001
Posts: 1970
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...


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Jul 11, 2001
Posts: 14112
In almost any case, it is save to assume that method calls don't take any time - *especially* if the method itself is complex!


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
Stephen Bloch
Ranch Hand

Joined: Aug 19, 2003
Posts: 48
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....)


  • SCJP 1.4
    Mark Lybarger
    Ranch Hand

    Joined: Dec 19, 2003
    Posts: 72
    i am interested in how static methods show a failed OO design?
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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

    Joined: Jun 26, 2002
    Posts: 860
    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!


    http://www.jamonapi.com/ - a fast, free open source performance tuning api.
    JavaRanch Performance FAQ
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    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.


    "I'm not back." - Bill Harding, Twister
    steve souza
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 860
    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

    Joined: Mar 19, 2008
    Posts: 1
    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

    Joined: Oct 30, 2001
    Posts: 1970
    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

    Joined: Mar 26, 2008
    Posts: 2
    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

    Joined: Sep 20, 2001
    Posts: 666
    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.


    BJ - SCJP and SCWCD
    We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

    Crazy Bikes created by m-Power
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Static vs Instance
     
    Similar Threads
    what did i break? "no main method found"
    Static Variable Problem
    static string
    static / non-static method overriding
    Static methods - overriding