Does any expression set slow compile time / execution speed? (In situations where one could have avoided)

In countless (well, numerical, but many) cases, especially in the method / function of the class, I was in a situation where I want to make a set of operations in the void-return function, but only if(condition met). In most cases, I see how (assuming the code works) the statement elsecan be deleted completely by simply returning to the block if.

Here is a concrete example if that doesn't make sense:

With an else expression (as a teacher would show it)

    private void ifThisDoThat(params){
        if(dependenciesNotMet) return;
        else{
            ////DO STUFF HERE...
        }
    }

No (more mean)

    private void ifThisDoThat(params){
        if(dependenciesNotMet) return;
        //Assuming the above does not execute, DO STUFF HERE...
    }

I think that deleting an instruction else, if there was optimization at all, will be classified as micro-optimization, but nevertheless decided that I would ask for my own edification.

Finally:

- return else?

, else?

else ( - )?

+5
5

. , .

, , . - :

  • - /, . , , , , , ​​ , . .
  • "" , if, , , /then/.
  • " ".

, , - :

  • , if, .
  • , " " .
  • (, ) .
+5

. , :

public void x(int i){
    if(i == 0){
        System.out.println("zero");
        return;
    }
    System.out.println("not zero");
}

public void y(int i){
    if(i == 0){
        System.out.println("zero");
        return;
    }
    else {
        System.out.println("not zero");
    }
}

( javap -v <class>):

x:

  public void x(int);
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: iload_1       
         1: ifne          13
         4: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
         7: ldc           #22                 // String zero
         9: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        12: return        
        13: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
        16: ldc           #30                 // String not zero
        18: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        21: return        
      LineNumberTable:
        line 6: 0
        line 7: 4
        line 8: 12
        line 10: 13
        line 11: 21
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0      22     0  this   Lpt/kash/Test;
               0      22     1     i   I
      StackMapTable: number_of_entries = 1
           frame_type = 13 /* same */

y:

  public void y(int);
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: iload_1       
         1: ifne          13
         4: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
         7: ldc           #22                 // String zero
         9: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        12: return        
        13: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
        16: ldc           #30                 // String not zero
        18: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        21: return        
      LineNumberTable:
        line 14: 0
        line 15: 4
        line 16: 12
        line 19: 13
        line 21: 21
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0      22     0  this   Lpt/kash/Test;
               0      22     1     i   I
      StackMapTable: number_of_entries = 1
           frame_type = 13 /* same */

- ... . , .

, ( ):

+2

. . .

, , , - else.

+1

There is no difference in performance, it will be optimized for the same thing, but for readability I recommend this (using one return principle):

private void ifThisDoThat(params){
    if(!dependenciesNotMet) { // would be nicer to change this to if(dependenciesNotMet)
        ////DO STUFF HERE...
    }
}
0
source

Testing the program using javap, I hardly see a difference, except for the JUMP instruction 14: goto 25in the second case.

I see no reason to add else if you have nothing to do in the else statement. You can use it, however, for logging, etc.

public class Test {
    public static void main(String[] s){
        if(testMethod()){
            System.out.println("in if");
        }
        System.out.println("in else");      
    }   
    static boolean testMethod(){
        return false;
    }
}

leads to

    public static void main(java.lang.String[]);
      Code:
       Stack=2, Locals=1, Args_size=1
       0:   invokestatic    #16; //Method testMethod:()Z
       3:   ifeq    14
       6:   getstatic       #20; //Field java/lang/System.out:Ljava/io/PrintStream;
       9:   ldc     #26; //String in if
       11:  invokevirtual   #28; //Method java/io/PrintStream.println:
                            (Ljava/lang/String;)V
       14:  getstatic       #20; //Field java/lang/System.out:Ljava/io/PrintStream;
       17:  ldc     #34; //String in else
       19:  invokevirtual   #28; 
      //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   22:  return

and

public class Test {
    public static void main(String[] s){
        if(testMethod()){
            System.out.println("in if");
        }else{
            System.out.println("in else");
        }
    }   
    static boolean testMethod(){
        return false;
    }
}

will produce

public static void main(java.lang.String[]);
  Code:
   Stack=2, Locals=1, Args_size=1
   0:   invokestatic    #16; //Method testMethod:()Z
   3:   ifeq    17
   6:   getstatic       #20; //Field java/lang/System.out:Ljava/io/PrintStream;
   9:   ldc     #26; //String in if
   11:  invokevirtual   #28; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   14:  goto    25
   17:  getstatic       #20; //Field java/lang/System.out:Ljava/io/PrintStream;
   20:  ldc     #34; //String in else
   22:  invokevirtual   #28; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   25:  return
0
source

All Articles