0

Is there any difference in runtime? if there is, why is that?

int pairSumSequence(int n) { 
    int sum = 0;
    for (int i = 0; i <  n; i++){
        sum += pairSum(i, i+1);
    }
    return sum;
}

int pairSum(int a, int b) {
    return a + b;
}

vs

int pairSumSequence(int n) { 
    int sum = 0;
    for (int i = 0; i <  n; i++){
        sum += i + (i + 1); //2 * i + 1
    }
    return sum;
}

Thank you!

  • In almost all cases, no; and if there is any difference, it's negligible. The JIT can optimize the difference away. Go for the one you find most readable. – Andy Turner Jan 14 '20 at 11:22
  • 2
    You can also use `n*(n+1)-n` instead of the loop. – Guy Jan 14 '20 at 11:28
  • 1
    @Someprogrammerdude And you probably won't see any difference or the results will be incorrect. You probably know that but your comment might be missleading. [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Amongalen Jan 14 '20 at 11:30
  • 1
    There should be a little difference because you call a new method which always needs some time (method stack), but to split your code in many small and logic methods helps you to have a cleaner code. It's also easier to test small methods. – CodingSamples Jan 14 '20 at 11:42
  • Thank you, guys! Those codes were just examples. I just wanted to know whether there is a huge difference in runtime if I call a new method to write a cleaner code rather than putting a bunch of codes in a loop. I guess it doesn't matter then – Hojin Park Jan 14 '20 at 12:21
  • Of course, generally speaking, a method call has its own and significant cost in not inlined https://www.baeldung.com/jvm-method-inlining . You can check this with -XX:+PrintInlining – AnatolyG Jan 20 '20 at 14:06

1 Answers1

0

Is there any difference in runtime?

Almost unnoticeable and it highly depends on optimisations that could be made. The compiler may inline the call (replace it by the equivalent expression as you did). Even the JIT-compiler may do it.

If there is, why is that?

Exactly as in real life! When you evaluate the first form, then you have to jump to the function, compute it and then jump back to the call point: this takes time and the machine doesn't nothing more than you. You have to pay the call. Calling a function is not very costly, so for many applications it has no serious impact on performance; of course if you are in some specific domain that can change, think of high-computation tasks as forecasting, simulating, etc.

So, to conclude, write the call form as this factorizes the code, leading to a more readable code, etc. Let compilers make optimizations if possible (compilers are very smart now). I doubt you will need to optimize such by yourself.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • @user85421 This is for Java Hotspot and/or Java legacy compilers. But there is no spec were you would read that it is forbidden for a java compiler to inline function call in some cases. – Jean-Baptiste Yunès Jan 14 '20 at 13:13
  • I would not except any compiler to inline methods at compile time, as it would be very hard to support all the required features, just like 13.4.22 part of specification states "It is still possible that a new version of that method will be provided at link-time. Furthermore, the structure of the original program must be preserved for purposes of reflection." – GotoFinal Jan 15 '20 at 10:53