【java performance】使用移位操作代替'a * b'

来源:互联网 发布:王者荣耀 嬴政 知乎 编辑:程序博客网 时间:2024/05/29 04:55

但除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在做什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。

 

例子:

public class SMUL {

   public void calculate(int a) {

        int mul = a * 4;            // should be replaced with "a<< 2".

       int mul2 = 8 * a;         //should be replaced with "a << 3".

       int temp = a * 3;

    }

}

 

更正:

package OPT;

public class SMUL {

   public void calculate(int a) {

       int mul = a << 2; 

       int mul2 = a << 3;

       int temp = a * 3;       // 不能转换

    }

}
0 0
原创粉丝点击