0

I wonder whether programming language's own method performs better than any other set of instructions written? To be more precise, here is an illustration for my question below.

1)max = (a > b)?a:b; // written by me 
2) max(a, b);    // method in Java

Which of the operations given above more efficient? What if we get this notion in general, for all other methods and the codes which gives the same result for a particular purpose?

Nodirxon
  • 99
  • 9

1 Answers1

1

It depends.

  1. The implementation of the "standard" method might have an implementation that is more (or less) efficient than yours
  2. The "standard" method might be executed a lot of time by other pieces of code (the JDK classes itself, or libraries), making it a hot method that is inlined and/or compiled by the JIT, making it faster than yours, called less often
  3. For some specific methods of some classes of the JDK, the method might in fact have an implementation in native code directly in the JVM, which could make it faster than your implementation.

But other than that, no, there is no special treatment for JDK methods in general. They're just Java code, like yours.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This should be an answer to the questin that this question duplicates (https://stackoverflow.com/questions/2103606/is-math-maxa-b-or-abab-faster-in-java) – Raedwald Dec 11 '18 at 17:51
  • 2
    Actually, this question shouldn't be a duplicate: the max() example is just an example, and the OP clearly says that it doesn't just wonder about that specific method, but about methods in general. – JB Nizet Dec 11 '18 at 17:54