【java performance】使用移位操作来代替'a / b'操作
2014-02-21 16:07
204 查看
"/"是一个很“昂贵”的操作,使用移位操作将会更快更有效。
例子:
public class SDIV {
public static final int NUM = 16;
public void calculate(int a) {
int div = a / 4; //should be replaced with "a >> 2".
int div2 = a / 8; //should be replaced with "a >> 3".
int temp = a / 3;
}
}
更正:
public class SDIV {
public static final int NUM = 16;
public void calculate(int a) {
int div = a >> 2;
int div2 = a >> 3;
int temp = a / 3; // 不能转换成位移操作
}
}
例子:
public class SDIV {
public static final int NUM = 16;
public void calculate(int a) {
int div = a / 4; //should be replaced with "a >> 2".
int div2 = a / 8; //should be replaced with "a >> 3".
int temp = a / 3;
}
}
更正:
public class SDIV {
public static final int NUM = 16;
public void calculate(int a) {
int div = a >> 2;
int div2 = a >> 3;
int temp = a / 3; // 不能转换成位移操作
}
}
相关文章推荐
- 【java performance】使用移位操作代替'a * b'
- 【java performance】使用'System.arraycopy ()'代替通过来循环复制数组
- 【java performance】用'StringTokenizer'代替 'indexOf()' 和'substring()'
- 【java performance】对于常量字符串,用'String' 代替'StringBuffer'
- JDK1.5使用总结 --《Java 1.5 Tiger: A Developer's Notebook》
- JDK1.5使用总结 --《Java 1.5 Tiger: A Developer's Notebook》
- 在sqlite中执行'in'查询操作,如何使用selectionargs匹配多个值
- java file文件类操作使用方法大全
- Java中布尔类型操作&=,|=与^=的使用
- How to improve Java's I/O performance( 提升 java i/o 性能)
- Java中布尔类型操作&=,|=与^=的使用
- JSP页面使用EL表达式出现的问题:javax.el.PropertyNotFoundException: Property 'ID' not found on type java.lang.Str
- Java 移位操作>>,<<,>>> 位逻辑运算符 &, |,~,^
- Microsoft JET Database Engine 错误 '80004005' 操作必须使用一个可更新的查询。问题解决办法
- [Sqlite]-->Java使用jdbc连接Sqlite数据库进行各种数据操作的详细过程
- 使用tr -d 'xyz' < file代替cat file | tr -d 'xyz'
- 使用jdbc连接mysql 出现 java.sql.Exception: this specifer does...(root@'%')..doesn't exist.
- java 'keytool' 命令使用
- JAVA 使用哈希表操作数据库的例子 Using Hashtables to Store & Extract results from a Database.
- Java使用synchronized实现多线程操作list<1>