没有合适的资源?快使用搜索试试~ 我知道了~
java程序性能优化好文章
5星 · 超过95%的资源 需积分: 10 12 下载量 17 浏览量
2013-05-22
22:44:51
上传
评论
收藏 382KB PDF 举报
温馨提示
试读
25页
java程序编写技巧,提升性能好经验,来自网上别人总结的整理。比较实用。
资源推荐
资源详情
资源评论
一、避免在循环条件中使用复杂表达式
在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循
环条件值不变的话,程序将会运行的更快。
例子:
import java.util.vector;
class cel {
void method (vector vector) {
for (int i = 0; i < vector.size (); i++) // violation
; // ...
}
}
更正:
class cel_fixed {
void method (vector vector) {
int size = vector.size ()
for (int i = 0; i < size; i++)
; // ...
}
}
二、为'vectors' 和 'hashtables'定义初始大小
jvm 为 vector 扩充大小的时候需要重新创建一个更大的数组,将原原先数组中的内容复制过
来,最后,原先的数组再被回收。可见 vector 容量的扩大是一个颇费时间的事。
通常,默认的 10 个元素大小是不够的。你最好能准确的估计你所需要的最佳大小。
例子:
import java.util.vector;
public class dic {
public void addobjects (object[] o) {
// if length > 10, vector needs to expand
for (int i = 0; i< o.length;i++) {
v.add(o); // capacity before it can add more elements.
}
}
public vector v = new vector(); // no initialcapacity.
}
更正:
自己设定初始大小。
public vector v = new vector(20);
public hashtable hash = new hashtable(10);
参考资料:
dov bulka, "java performance and scalability volume 1: server-side programming
techniques" addison wesley, isbn: 0-201-70429-3 pp.55 – 57
三、在 finally 块中关闭 stream
程序中使用到的资源应当被释放,以避免资源泄漏。这最好在 finally 块中去做。不管程序执行
的结果如何,finally 块总是会执行的,以确保资源的正确关闭。
例子:
import java.io.*;
public class cs {
public static void main (string args[]) {
cs cs = new cs ();
cs.method ();
}
public void method () {
try {
fileinputstream fis = new fileinputstream ("cs.java");
int count = 0;
while (fis.read () != -1)
count++;
system.out.println (count);
fis.close ();
} catch (filenotfoundexception e1) {
} catch (ioexception e2) {
}
}
}
更正:
在最后一个 catch 后添加一个 finally 块
参考资料:
peter haggar: "practical java - programming language guide".
addison wesley, 2000, pp.77-79
四、使用'system.arraycopy ()'代替通过来循环复制数组
'system.arraycopy ()' 要比通过循环来复制数组快的多。
例子:
public class irb
{
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
for (int i = 0; i < array2.length; i++) {
array2 [i] = array1 [i]; // violation
}
}
}
更正:
public class irb
{
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
system.arraycopy(array1, 0, array2, 0, 100);
}
}
参考资料:
http://www.cs.cmu.edu/~jch/java/speed.html
五、让访问实例内变量的 getter/setter 方法变成”final”
简单的 getter/setter 方法应该被置成 final,这会告诉编译器,这个方法不会被重载,所以,可
以变成”inlined”
例子:
class maf {
public void setsize (int size) {
_size = size;
}
private int _size;
}
更正:
class daf_fixed {
final public void setsize (int size) {
_size = size;
}
private int _size;
}
参考资料:
warren n. and bishop p. (1999), "java in practice", p. 4-5
addison-wesley, isbn 0-201-36065-9
六、避免不需要的 instanceof 操作
如果左边的对象的静态类型等于右边的,instanceof 表达式返回永远为 true。
例子:
public class uiso {
public uiso () {}
}
class dog extends uiso {
void method (dog dog, uiso u) {
dog d = dog;
if (d instanceof uiso) // always true.
system.out.println("dog is a uiso");
uiso uiso = u;
if (uiso instanceof object) // always true.
system.out.println("uiso is an object");
}
}
更正:
删掉不需要的 instanceof 操作。
class dog extends uiso {
void method () {
dog d;
system.out.println ("dog is an uiso");
system.out.println ("uiso is an uiso");
}
}
七、避免不需要的造型操作
所有的类都是直接或者间接继承自 object。同样,所有的子类也都隐含的“等于”其父类。那么,
由子类造型至父类的操作就是不必要的了。
例子:
class unc {
string _id = "unc";
}
class dog extends unc {
void method () {
dog dog = new dog ();
unc animal = (unc)dog; // not necessary.
object o = (object)dog; // not necessary.
}
}
更正:
class dog extends unc {
void method () {
dog dog = new dog();
unc animal = dog;
object o = dog;
}
}
参考资料:
nigel warren, philip bishop: "java in practice - design styles and idioms
for effective java". addison-wesley, 1999. pp.22-23
八、如果只是查找单个字符的话,用 charat()代替
startswith()
用一个字符作为参数调用 startswith()也会工作的很好,但从性能角度上来看,调用用 string
api 无疑是错误的!
例子:
public class pcts {
private void method(string s) {
if (s.startswith("a")) { // violation
// ...
}
}
}
更正
将'startswith()' 替换成'charat()'.
public class pcts {
private void method(string s) {
if ('a' == s.charat(0)) {
// ...
}
}
}
参考资料:
dov bulka, "java performance and scalability volume 1: server-side programming
techniques" addison wesley, isbn: 0-201-70429-3
九、使用移位操作来代替'a / b'操作
"/"是一个很“昂贵”的操作,使用移位操作将会更快更有效。
例子:
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; // 不能转换成位移操作
}
}
十、使用移位操作代替'a * b'
剩余24页未读,继续阅读
资源评论
- leadery1102013-07-27资源很不错,谢谢
hanren008
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功