java how expensive is a method call

后端 未结 12 1558
情书的邮戳
情书的邮戳 2020-11-27 04:04

I\'m a beginner and I\'ve always read that it\'s bad to repeat code. However, it seems that in order to not do so, you would have to have extra method calls usually. Let\'s

相关标签:
12条回答
  • 2020-11-27 04:54

    I would definitely leave it as is. What if you change the clear() logic? It would be impractical to find all the places where you copied the 2 lines of code.

    0 讨论(0)
  • 2020-11-27 04:54

    The best practice is to measure twice and cut once.

    Once you've wasted time optimization, you can never get it back again! (So measure it first and ask yourself if it's worth optimisation. How much actual time will you save?)

    In this case, the Java VM is probably already doing the optimization you are talking about.

    0 讨论(0)
  • 2020-11-27 04:57

    What everyone else has said about optimization is absolutely true.

    There is no reason from a performance point of view to inline the method. If it's a performance issue, the JIT in your JVM will inline it. In java, method calls are so close to free that it isn't worth thinking about it.

    That being said, there's a different issue here. Namely, it is bad programming practice to call an overrideable method (i.e., one that is not final, static, or private) from the constructor. (Effective Java, 2nd Ed., p. 89 in the item titled "Design and document for inheritance or else prohibit it")

    What happens if someone adds a subclass of BinarySearchTree called LoggingBinarySearchTree that overrides all public methods with code like:

    public void clear(){
      this.callLog.addCall("clear");
      super.clear();
    }
    

    Then the LoggingBinarySearchTree will never be constructable! The issue is that this.callLog will be null when the BinarySearchTree constructor is running, but the clear that gets called is the overridden one, and you'll get a NullPointerException.

    Note that Java and C++ differ here: in C++, a superclass constructor that calls a virtual method ends up calling the one defined in the superclass, not the overridden one. People switching between the two languages sometimes forget this.

    Given that, I think it's probably cleaner in your case to inline the clear method when called from the constructor, but in general in Java you should go ahead and make all the method calls you want.

    0 讨论(0)
  • 2020-11-27 04:58

    Keep the clear() method when it helps readability. Having unmaintainable code is more expensive.

    0 讨论(0)
  • 2020-11-27 05:07

    Generally speaking (and as a beginner this means always!) you should never make micro-optimisations like the one you're considering. Always favour readability of code over things like this.

    Why? Because the compiler / hotspot will make these sorts of optimisations for you on the fly, and many, many more. If anything, when you try and make optimisations along these sorts of lines (though not in this case) you'll probably make things slower. Hotspot understands common programming idioms, if you try and do that optimisation yourself it probably won't understand what you're trying to do so it won't be able to optimise it.

    There's also a much greater maintenance cost. If you start repeating code then it's going to be much more effort to maintain, which will probably be a lot more hassle than you might think!

    As an aside, you may get to some points in your coding life where you do need to make low level optimisations - but if you hit those points, you'll definitely, definitely know when the time comes. And if you don't, you can always go back and optimise later if you need to.

    0 讨论(0)
  • 2020-11-27 05:07

    The pattern that I follow, is whether or not this method in question would satisfy one of the following:

    • Would it be helpful to have this method available outside this class?
    • Would it be helpful to have this method available in other methods?
    • Would it be frustrating to rewrite this every time i needed it?
    • Could the versatility of the method be increased with the use of a few parameters?

    If any of the above are true, it should be wrapped up in it's own method.

    0 讨论(0)
提交回复
热议问题