1

I have a question about calling a method in if-else or out of if-else statement.

Calling method in if-else:

int a = 1;

if (SOME_CONDITION) {
    /* Never chagned variable a */
    foo(a);
} else {
    /* Never chagned variable a */
    foo(a);
}

Calling method out of if-else:

int a = 1;

if (SOME_CONDITION) {
    /* Never chagned variable a */
} else {
    /* Never chagned variable a */
}

foo(a);

Which one has better performance?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Chris
  • 392
  • 4
  • 16
  • Try it: [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). But honestly, I wouldn't think there is much of a difference. I would make the decision based on readability: Do the parameters depend on the if? If not, why is the `a` defined before the `if`, but `foo` only called later? Should the content of the `if/else` be extracted into separate methods... – Malte Hartwig Aug 14 '18 at 09:26
  • Both would be identical. JVM by itself does a lot of optimisation. It would have inlined the method. – Nishit Aug 14 '18 at 09:26
  • 2
    You shouldn't be worried about performance here (unless this really is a bottleneck). You should be focused in readability and (elimination of) code duplication. – Mick Mnemonic Aug 14 '18 at 09:26
  • If you don't need foo involved before exiting the entire IF/ELSE block then it's a better idea to have a single call to foo(a). The other way around you will end up with code duplication and it will look sneaky. I'm pretty sure both implementations will be compiled to the very same bytecode but not hundred percent sure ;) – dbl Aug 14 '18 at 09:27

6 Answers6

4

There is no difference in the performance but in the code duplication. Thus the second way is better.

However, let me introduce you another way:

if (SOME_CONDITION) {
   /* Never chagned variable a */
 } else {
    /* Never chagned variable a */
}

int a = 1;
foo(a);

Or even better:

foo(1);

Keep the variable with the method that calls it if there are no steps between. Also, don't define a new variable since the value might be passed immediately.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
1

Which one has better performance?

Either way, the method is only called once, so it won't make any difference.

In general, though, focus on writing clear, understandable, maintainable code, and worry about a performance problem if and when you have a specific performance problem to worry about.

In this case, your second option is clearer and more easily maintained (having the call in two places opens up the possibility of changing one of them and forgetting to change the other).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Performance wise, both are same.

Talking about memory, Second way is better. This method is known as code movement and is a code optimization technique. This is because if the function is made inline by compiler, then whole function body will be pasted twice which will lead to more code space. Hope that helps :)

Krishna Choudhary
  • 615
  • 1
  • 5
  • 15
1

Both of them have same performance.

superCoder
  • 21
  • 2
0

There is no determinable difference, but option 2 is better code, due to the fact that foo() isn't depending on the condition

Korni
  • 464
  • 2
  • 10
-1

Programming itself suggests, one line is better than 2 lines. but in this case

if (SOME_CONDITION) {
/* Never chagned variable a */
foo(a);}

IT WILL HAVE NO SIGNIFICANT IMPACT IN YOUR PERFORMACE AS IF CONDITION EXECUTES JUST ONCE

Anish
  • 155
  • 1
  • 13