Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java how expensive is a method call

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 say I have the following class

public class BinarySearchTree<E extends Comparable<E>>{     private BinaryTree<E> root;     private final BinaryTree<E> EMPTY = new BinaryTree<E>();     private int count;     private Comparator<E> ordering;      public BinarySearchTree(Comparator<E> order){         ordering = order;         clear();     }      public void clear(){         root = EMPTY;         count = 0;     } } 

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? If so how much of a difference does it make? What if my constructor made 10 method calls with each one simply setting an instance variable to a value? What's the best programming practice?

like image 337
jhlu87 Avatar asked Jun 27 '11 15:06

jhlu87


People also ask

What is a Java method call?

The MethodCall operator is used to call an arbitrary Java method using the Java reflection API. Two forms of method call are supported, static and instance level. For static method calls Input 1 should be set to null and Inputs 2,3,4 are required.

What happens when you call a method?

When a method is invoked (called), a request is made to perform some action, such as setting a value, printing statements, returning an answer, etc. The code to invoke the method contains the name of the method to be executed and any needed data that the receiving method requires.

How many ways we can call a method?

Method Calling There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value). the return statement is executed.

Can we call method in method?

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.


1 Answers

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.

like image 195
Vineet Reynolds Avatar answered Sep 20 '22 14:09

Vineet Reynolds