java how expensive is a method call

后端 未结 12 1543
情书的邮戳
情书的邮戳 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:44

    Would it be more optimal for me to just copy and paste the two lines in my clear() method into the constructor instead of calling the actual method?

    The compiler can perform that optimization. And so can the JVM. The terminology used by compiler writer and JVM authors is "inline expansion".

    If so how much of a difference does it make?

    Measure it. Often, you'll find that it makes no difference. And if you believe that this is a performance hotspot, you're looking in the wrong place; that's why you'll need to measure it.

    What if my constructor made 10 method calls with each one simply setting an instance variable to a value?

    Again, that depends on the generated bytecode and any runtime optimizations performed by the Java Virtual machine. If the compiler/JVM can inline the method calls, it will perform the optimization to avoid the overhead of creating new stack frames at runtime.

    What's the best programming practice?

    Avoiding premature optimization. The best practice is to write readable and well-designed code, and then optimize for the performance hotspots in your application.

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

    The cost of a method call is the creation (and disposal) of a stack frame and some extra byte code expressions if you need to pass values to the method.

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

    Optimizing compilers usually do a pretty good job of removing the redundancy from these "extra" operations; in many instances, the difference between "optimized" code and code simply written the way you want, and run through an optimizing compiler is none; that is to say, the optimizing compiler usually does just as good a job as you'd do, and it does it without causing any degradation of the source code. In fact, many times, "hand-optimized" code ends up being LESS efficient, because the compiler considers many things when doing the optimization. Leave your code in a readable format, and don't worry about optimization until a later time.

    "Premature optimization is the root of all evil." - Donald Knuth

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

    As others have said, the cost of the method call is trivial-to-nada, as the compiler will optimize it for you.

    That said, there are dangers in making method calls to instance methods from a constructor. You run the risk of later updating the instance method so that it may try to use an instance variable that has not been initiated yet by the constructor. That is, you don't necessarily want to separate out the construction activities from the constructor.

    Another question--your clear() method sets the root to EMPTY, which is initialized when the object is created. If you then add nodes to EMPTY, and then call clear(), you won't be resetting the root node. Is this the behavior you want?

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

    Given the memory of modern computers this is very inexpensive. Its always better to break your code up into methods so someone can quickly read whats going on. It will also help with narrowing down errors in the code if the error is restricted to a single method with a body of a few lines.

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

    I wouldn't worry about method call as much but the logic of the method. If it was critical systems, and the system needed to "be fast" then, I would look at optimising codes that takes long to execute.

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