java - if 和逻辑运算符之间的性能差异是什么

标签 java performance

我想知道当我使用逻辑运算符而不是多个 if 语句时是否会有性能差异。我看到一个不错的link ,这也适用于java吗?

最佳答案

我刚刚创建了类似的类

class Test{
    static java.util.Random r=new java.util.Random();
    boolean test(){
        return r.nextBoolean();
    }
    void test1(){
        if (test() && test() && test())
            System.out.println("3x yes");
    }
    void test2(){
        if (test())
            if (test()) 
                if (test())
                    System.out.println("3x yes");
    }
}

编译它然后通过javap -c Test反编译并得到这些结果

class Test {
  static java.util.Random r;

  Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":
()V
       4: return

  boolean test();
    Code:
       0: getstatic     #2                  // Field r:Ljava/util/Random;
       3: invokevirtual #3                  // Method java/util/Random.nextBoole
an:()Z
       6: ireturn

  void test1();
    Code:
       0: aload_0
       1: invokevirtual #4                  // Method test:()Z
       4: ifeq          29
       7: aload_0
       8: invokevirtual #4                  // Method test:()Z
      11: ifeq          29
      14: aload_0
      15: invokevirtual #4                  // Method test:()Z
      18: ifeq          29
      21: getstatic     #5                  // Field java/lang/System.out:Ljava/
io/PrintStream;
      24: ldc           #6                  // String 3x yes
      26: invokevirtual #7                  // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V
      29: return

  void test2();
    Code:
       0: aload_0
       1: invokevirtual #4                  // Method test:()Z
       4: ifeq          29
       7: aload_0
       8: invokevirtual #4                  // Method test:()Z
      11: ifeq          29
      14: aload_0
      15: invokevirtual #4                  // Method test:()Z
      18: ifeq          29
      21: getstatic     #5                  // Field java/lang/System.out:Ljava/
io/PrintStream;
      24: ldc           #6                  // String 3x yes
      26: invokevirtual #7                  // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V
      29: return

  static {};
    Code:
       0: new           #8                  // class java/util/Random
       3: dup
       4: invokespecial #9                  // Method java/util/Random."<init>":
()V
       7: putstatic     #2                  // Field r:Ljava/util/Random;
      10: return
}

可以看到test1test2的字节码是相同的,所以使用上没有区别

    if (test() && test() && test())

    if (test())
        if (test()) 
            if (test())

关于java - if 和逻辑运算符之间的性能差异是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11677717/

相关文章:

java - Spring Boot 中的 TransactionRequiredException

java - 使用 Android Room 从 Junction Table 获取数据

performance - Jmeter :- Running a bundle of samplers sequentially within concurrent threads

python - 'max area of islands' 算法的 3D 实现期间核心转储

java - 用户与当前数据匹配

Java Maven 项目 - 错误 R10(启动超时)-> Web 进程在启动后 90 秒内无法绑定(bind)到 $PORT

performance - Drupal 7 是否比 Drupal 6 慢?

ruby-on-rails - etags和服务器群

performance - 如何最大化原生 R 脚本的性能(将运行数千次)?

java - 如果任何请求在 tomcat 线程池中创建更多线程怎么办