Force
Force

Reputation: 6382

What would be more efficient in java? If-return-else or If-return?

I was just wondering, what is actually more efficient for the compiler to use?

if (condition) {
    return something;
} else {
    dosomething();      
}

OR

if (condition) {
    return something;
}
dosomething();

I know that these are extremely low differences and that the more readable version should be chosen, but lets just say we would execute this code a billion times, which one would be more efficient?

I also know that there are no gains at all in the "real world", but I am just curious.

Upvotes: 1

Views: 572

Answers (6)

Mike Clark
Mike Clark

Reputation: 10136

No performance difference. The bytecode contains the same instructions, in the same order, operating on the same data.

   L0
    LINENUMBER 11 L0
    ICONST_1
    ISTORE 0
   L1
    LINENUMBER 12 L1
    ILOAD 0
    IFEQ L2
   L3
    LINENUMBER 13 L3
    GETSTATIC p/A.something : Ljava/lang/Object;
    ARETURN
   L2
    LINENUMBER 15 L2
   FRAME APPEND [I]
    INVOKESTATIC p/A.dosomething()V
   L4
    LINENUMBER 17 L4
    ACONST_NULL
    ARETURN
   L5
    LOCALVARIABLE condition Z L1 L5 0
    MAXSTACK = 1
    MAXLOCALS = 1

vs.

   L0
    LINENUMBER 7 L0
    ICONST_1
    ISTORE 0
   L1
    LINENUMBER 8 L1
    ILOAD 0
    IFEQ L2
   L3
    LINENUMBER 9 L3
    GETSTATIC p/B.something : Ljava/lang/Object;
    ARETURN
   L2
    LINENUMBER 11 L2
   FRAME APPEND [I]
    INVOKESTATIC p/B.dosomething()V
   L4
    LINENUMBER 12 L4
    ACONST_NULL
    ARETURN
   L5
    LOCALVARIABLE condition Z L1 L5 0
    MAXSTACK = 1
    MAXLOCALS = 1

public class A {
    static Object something = new Object();
    public static void main(String[] args) { 
        test(); 
    }
    private static Object test() {
        boolean condition = true;
        if (condition) {
            return something;
        } else {
            dosomething();
        }
        return null;
    }
    private static void dosomething() {}
}

vs.

public class B {
    static Object something = new Object();
    public static void main(String[] args) { 
        test(); 
    }
    private static Object test() {
        boolean condition = true;
        if (condition) { 
            return something; 
        }
        dosomething();
        return null;
    }
    private static void dosomething() {}
}

Upvotes: 7

Mitch Wheat
Mitch Wheat

Reputation: 300489

Any gains would be negligible, and I wouldn't be surprised if the resulting instructions are identical.

You are almost always better off concentrating on 'low hanging fruit', rather than such small performance micro-optimisations.

Err on the side of readability. And always measure to ensure you really have a performance bottleneck.

Upvotes: 1

user882347
user882347

Reputation:

I believe that it's just personal preference.

Some more informational answers can be found at this question

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258548

There is no difference whatsoever in the two versions. Only differences that may be are purely from a readability point of view.

If you adopt a style where checks are done at the beginning of a function and return is executed as soon as possible, the second comes in hand.

void foo()
{
   if (condition1)
      return;
   if (goo())
      return;
   //...
}

Otherwise, the first version provides more visible branching.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881103

I doubt there's much of a difference in performance but I tend to prefer the latter since the former can lead to indentation hell.

Even executing that code a billion times, the doSomething() is almost certainly going to swamp the if/else in terms of time taken.

I always optimise for readability first and only worry about performance when it becomes a problem.

Unreadable code is far more of an issue than slightly slow code :-)

Upvotes: 1

Cameron S
Cameron S

Reputation: 2301

It wouldn't make a significant difference at all. Realize that the else is not evaluating another expression, but simply saying if the original condition is false, do this. Condition is still evaluated once both ways.

As a matter of fact, depending on the optimization done by different compilers, it may result in the same bytecode. There are many plugins for IDE's to view Java bytecode.

Upvotes: 1

Related Questions