-1

Inline methods (like in C++) seem to be a very simple idea and help to optimize code.
For example (averaged over 1'000'000 cycles):

float dx = a - c, dy = b - d;
float distance = (float) Math.sqrt(dx * dx + dy * dy);

is 4(!) times faster then calling method with the same content:

   float distance = getDistance(a,b,c,d);

So there must be a reason that Java doesn't support inline methods.
What is it?

sberezin
  • 3,266
  • 23
  • 28

1 Answers1

4

I don't see a reason why this happens. Try to the method final and the difference should go away. And use the latest version of Java from Oracle or IBM.

Now the reason why Java doesn't have many optimization keywords is simple: The people who invented Java didn't like how much time C developers spent to squeeze out the last drop of juice out of the compiler. That felt like a huge waste of productive time.

So the idea with Java is that you write code. You concentrate just on writing good code. The Java compiler and the runtime then make it fast. That means that the runtime analyzes the CPU and optimizes the code differently depending on what the CPU can do. This is why Java code can be faster than C++.

Of course, bad code is bad, no matter how clever the optimizer is. So you can write code which very slow but that doesn't depend on the language.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820