下面的代码存在性能的问题,请指出,并改正:
public static int testroop(int rooptime){ String str = new String("testString"); for(int i = 0;i < rooptime;i ++){
char ch = 'a';
str += ch;
} return rooptime;
}
public static int testroop(int rooptime){ String str = new String("testString"); for(int i = 0;i < rooptime;i ++){
char ch = 'a';
str += ch;
} return rooptime;
}
str += ch;//要用str构造成StringBuffer类的,否则每次用+操作时都会系统生成一个StringBuffer实例,效率当然不行。看一下String类的源代码就行了。
public static int testroop(int rooptime){
String str = new String("testString");
StringBuffer sb = new StringBuffer(str);
char ch= 'a';
for(int i = 0;i < rooptime;i ++){
sb.append(ch);
} return rooptime;
}
char ch = 'a';
str += ch;
} return rooptime;
}
1.String类对象为常量对象,创建后值不可变,所以每次循环都是STR从新指向一个新的STRING类对象先前的哪个边垃圾,浪费内存
String str = new String("testString")这样其实创建了两个对象;改为String str = "testString";
循环里面应该用StringBffer
char ch = 'a';
StringBuffer bf= new StringBffer("");
for(int i = 0;i < rooptime;i ++){
bf.append(++ch);
}
return bt.toString();
2根据 rooptime分配内存
3循环对内存赋值而不是append
public static int testroop(int rooptime){
StringBuffer str = new StringBuffer("testString");
for(int i = 0;i < rooptime;i ++){
char ch = 'a';
str.append(ch);
}
return rooptime;
}
为什么要这样改?
String str = new String("testString"); for(int i = 0;i < rooptime;i ++){
char ch = 'a';
str += ch;
}
这段代码有什么用?外界又用不到str变量
char[] buffer = new char[str.length+rooptime];
System.arraycopy(buffer,0,str.toCharArray(),0,str.length());
Arrays.fill(buffer,str.length(),buffer.length,'a');
这样比用StringBuffer更有效
问题出在这个地方了
String是不能改变的 这样写的话会不停的产生新的对象
char ch = 'a';
str += ch;
}
*/都没用
return rooptime;
}
这一句也应该改成
String str = "testString";
我觉得这道题主要就是考String和StringBuffer的区别,这是初级面试题里面常考的一个问题,如果大家用DeBug进行跟踪就会发现String完成的"+"法其实做了很多事。而且String是基于“不变模式”的,也就是说每一次"+"后的结果其实是申请了一次内存资源,并将原有的资源回收,这里才是性能问题出现的地方
改为这个:
public static int testroop(int rooptime){
return rooptime;
}
public static int testroop(int rooptime){/* String str = new String("testString"); for(int i = 0;i < rooptime;i ++){
char ch = 'a';
str += ch;
}
*/都没用
return rooptime;
}
这个函数到底有什么垃圾用,传个参数进来再返回去,参数也没有发生什么变化,晕,方法都没有任何功能,讨论性能有何意义
赞一个! 我认同你的观点! 偶开始也被迷惑啦.faint~~
public static int testroop(int rooptime){ StringBuffer str = new StringBuffer("testString"); for(int i = 0;i < rooptime;i ++){
char ch = 'a';
str.append(ch);
} return rooptime;
}
public static int testroop(int rooptime){
String str = new String("testString");
StringBuffer sb = new StringBuffer(str);
char ch= 'a';
for(int i = 0;i < rooptime;i ++){
sb.append(ch);
} return rooptime;
}
StringBuffer sb = new StringBuffer("testString");
char ch= 'a';
for(int i = 0;i < rooptime;i ++){
sb.append(ch);
} return rooptime;
}
StringBuilder sb = new StringBuilder("testString");
char ch= 'a';
for(int i = 0;i < rooptime;i ++){
sb.append(ch);
} return rooptime;
}//stringBuilder 比stringbuffer 还好用.呵呵
问题都出在String上面String是不可变对象所以
String str = new String("testString")这样其实创建了两个对象;改为String str = "testString";
循环里面应该用StringBffer
char ch = 'a';
StringBuffer bf= new StringBffer("");
for(int i = 0;i < rooptime;i ++){
bf.append(++ch);
}
return bt.toString();
//第二:如果非要在这里面寻找性能的话问题的话。stringBuffer也不是最好的。
import java.util.*;
class testIt
{
public static int testroop(int rooptime){
String str = new String("testString");
char[] acTmp1 = str.toCharArray();
int iPos = acTmp1.length;
char[] acTmp2 = new char[iPos+rooptime];
System.arraycopy(acTmp1,0,acTmp2,0,iPos); char ch = 'a';
for(;iPos < rooptime;iPos ++){
acTmp2[iPos] = ch;
}
str = new String(acTmp2); return rooptime;
} //随便拷贝一段函数
public static int testroop2(int rooptime){ StringBuffer str = new StringBuffer("testString");
char ch = 'a';
for(int i = 0;i < rooptime;i ++){
str.append(ch);
}
return rooptime;
} public static void main(String[] args)
{
Date dt1 = new Date();
testroop(10000000);
Date dt2 = new Date();
System.out.println("cost1 =["+(dt2.getTime() - dt1.getTime())+"]"); Date dt3 = new Date();
testroop2(10000000);
Date dt4 = new Date();
System.out.println("cost2 =["+(dt4.getTime() - dt3.getTime())+"]");
}
}
// 两段同时编译的代码运行的时间比是 cost1:cost2 == 2:5 200:500
// 注释掉其中一段分别运行 cost1 =180 cost2 = 900