Should if(a&&b) take more time than if(a) if(b)?

南笙酒味 提交于 2020-01-06 19:33:39

问题


I used the following simple logic to answer a question like this one:

1:   if(a)        // 1 operation
2:   if (b)       // 1 operation

and

1:  if(a && b) // 1, 1(&&), 1 => 3 operations. 

So, 2 operations versus 3, but in the first example the compiler needs to call another instruction to be executed.

Is this logic true?. Does it depend on the compiler?. Does calling an empty instruction like only ; cost the compiler some noticable time?.

This also discuss the same problem but not considering this logic. Please help us to clarify this issue.


回答1:


There are two methods to answer such a question precisely:

1.) Look at the IL code (and/or) aassembly code produce and count the CPU cycles needed to execute this code (Hint: this is not for beginners)

2.) Build a small test programm which executes both variants a large number of time, use StopWatch() to create a uesful and readable timing output, run it several times.

3.) Speculate about what you think the optimization step of the compiler is able to do and what this software will do, argue with others for hours




回答2:


I assumed the compiler would produce the same byte code for your two cases. So I tested this with two different source files:

public class Test1 {    
  public static void main(String[] args) {
    if (args[0].equals("a"))
      if (args[1].equals("b"))
        System.out.println("Foo");
  }    
}

and...

public class Test2 {    
  public static void main(String[] args) {
    if (args[0].equals("a") && args[1].equals("b"))
      System.out.println("Foo");
  }    
}

Inspecting their byte code with javap -c Test1 etc., the results are identical:

  public static void main(java.lang.String[]);
    Code:
       0: aload_0
       1: iconst_0
       2: aaload
       3: ldc           #2                  // String a
       5: invokevirtual #3                  // Method java/lang/String.equals:(Ljava/lang/Object;)Z
       8: ifeq          30
      11: aload_0
      12: iconst_1
      13: aaload
      14: ldc           #4                  // String b
      16: invokevirtual #3                  // Method java/lang/String.equals:(Ljava/lang/Object;)Z
      19: ifeq          30
      22: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
      25: ldc           #6                  // String Foo
      27: invokevirtual #7                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      30: return

Consequently, the performance would be identical. Although I welcome comments if anyone can think of an example where different byte code is produced.

My results are using Oracle's javac from Java 1.7. Results could be different with other compilers, although I suspect they won't be for this case.




回答3:


There are 2 approaches to think about your question:

  1. the Java language definition

    • language defines that the 2nd example will use short-circuit execution which means in the case the if statements will contain any code, the 2nd example may execute faster
  2. the JVM optimization

    • the JVM runtime will remove the dead code blocks if it can proof they don't have any side-effects


来源:https://stackoverflow.com/questions/29033373/should-ifab-take-more-time-than-ifa-ifb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!