Will reassigning a variable at each iteration of the loop affect performance?

Consider two ways to write a loop in Java to see if a list contains a given value:

Style 1

boolean found = false;
for(int i = 0; i < list.length && !found; i++)
{
   if(list[i] == testVal)
     found = true;
}

Style 2

boolean found = false;
for(int i = 0; i < list.length && !found; i++)
{
   found = (list[i] == testVal);
}

Both are equivalent, but I always use the style 1, because 1) I find it more readable, and 2) I assume that reassignment foundto falsea hundred times it seems that it will take more time. I wonder: is this second assumption correct?

Nitpicker corner

  • I know well that this is a case of premature optimization. This does not mean that it is not that useful to know.
  • I don't care which style you think is more readable. I'm only interested in whether the performance metric is compared to the other.
  • , 1 , break; if, . , , .
+3
13

, -:

import java.util.*;

public class Test {
    private static int[] list = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9} ;
    private static int testVal = 6;


    public static boolean version1() {
        boolean found = false;
        for(int i = 0; i < list.length && !found; i++)
        {
        if(list[i] == testVal)
            found = true;
        }
        return found;

    }

    public static boolean version2() {
    boolean found = false;
    for(int i = 0; i < list.length && !found; i++)
        {
        found = (list[i] == testVal);
        }

    return found;
    }


    public static void main(String[] args) {

        // warm up
    for (int i=0; i<100000000; i++) {
        version1();
        version2();
    }


    long time = System.currentTimeMillis();
    for (int i=0; i<100000000; i++) {
        version1();
    }

    System.out.println("Version1:" + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    for (int i=0; i@lt;100000000; i++) {
        version2();
    }

        System.out.println("Version2:" + (System.currentTimeMillis() - time));
    }
}

1 :

Version1: 5236

Version2: 5477

( 0,2 100 . ).

-, 2 , , , :

public static boolean version1();
  Code:
   0:   iconst_0
   1:   istore_0
   2:   iconst_0
   3:   istore_1
   4:   iload_1
   5:   getstatic   #2; //Field list:[I
   8:   arraylength
   9:   if_icmpge   35
   12:  iload_0
   13:  ifne    35
   16:  getstatic   #2; //Field list:[I
   19:  iload_1
   20:  iaload
   21:  getstatic   #3; //Field testVal:I
   24:  if_icmpne   29
   27:  iconst_1
   28:  istore_0
   29:  iinc    1, 1
   32:  goto    4
   35:  iload_0
   36:  ireturn

public static boolean version2();
  Code:
   0:   iconst_0
   1:   istore_0
   2:   iconst_0
   3:   istore_1
   4:   iload_1
   5:   getstatic   #2; //Field list:[I
   8:   arraylength
   9:   if_icmpge   39
   12:  iload_0
   13:  ifne    39
   16:  getstatic   #2; //Field list:[I
   19:  iload_1
   20:  iaload
   21:  getstatic   #3; //Field testVal:I
   24:  if_icmpne   31
   27:  iconst_1
   28:  goto    32
   31:  iconst_0
   32:  istore_0
   33:  iinc    1, 1
   36:  goto    4
   39:  iload_0
   40:  ireturn
+8

nitpicks:

, "&! found" # 1. , .

,

boolean notFound = true;
    for(int i = 0; notFound && i < list.length; i++)
    {
       if(list[i] == testVal)
         notFound = false;
    }

, № 1.

, , , for. , , . break/continue, while do/while.

+3

, "if" , , - pipeline.

+2

, , .

+1

, 2 - - , 1 .

, , :

for(i=0; i<list.length && list[i]!=testval; i++);
boolean found = (i!=list.length);
+1

, , , # 2 - . , , ( , IMO), - :

1:

start:
  CMP i, list.length
  JE end
  CMP list[i], testval
  JE equal
  JMP start
equal:
  MOV true, found
end:

2:

start:
  CMP i, list.length
  JE end
  CMP true, found
  JE end
  CMP list[i], testval
  JE equal
  JNE notequal
equal:
  MOV true, found
  JMP start
notequal:
  MOV false, found
  JMP start
end:

, 1 , 1/3 . , , ( ? ?).

+1

for(int i = 0; i < list.length; i++)
{
   if(list[i] == testVal)
     return true;
}

return false;
+1

, , .

, ( ) .

Matt, , , ( , , ) . "" .

, , , - Java, , JVM-, .

+1

, 98% . , , , .

: , .

0

, .

, , ... , .

0

, (, , ..), , , , , .

0

, (, Sun) - ... , - .

0
boolean found = false;
for(int i = 0; i < list.length && !found; i++)
{
   if(list[i] == testVal)
     found = true;
}

break .

Other than that, I prefer this style. This improves readability and, therefore, the likelihood that the companion reads incorrectly and incorrectly captures it.

0
source

Source: https://habr.com/ru/post/1698053/


All Articles