StringBuffer upp = new StringBuffer(),
low = new StringBuffer();
for(int i = 0; i<=9000000; i++){
upp.append("AA");
low.append("aa");
}
long Tupp = System.currentTimeMillis();
upp.toString().toUpperCase();
System.out.println("UPP:"+(System.currentTimeMillis()-Tupp));
long Tlow = System.currentTimeMillis();
low.toString().toUpperCase();
System.out.println("LOW:"+(System.currentTimeMillis()-Tlow));这样执行N次,都是UPP<LOW;如果把上述的1个A变成a,即:用Aa和aa比较,则UPP>LOW,想知道这是为什么?看toUpperCase源码也没有明白。
Java
low = new StringBuffer();
for(int i = 0; i<=9000000; i++){
upp.append("AA");
low.append("aa");
}
long Tupp = System.currentTimeMillis();
upp.toString().toUpperCase();
System.out.println("UPP:"+(System.currentTimeMillis()-Tupp));
long Tlow = System.currentTimeMillis();
low.toString().toUpperCase();
System.out.println("LOW:"+(System.currentTimeMillis()-Tlow));这样执行N次,都是UPP<LOW;如果把上述的1个A变成a,即:用Aa和aa比较,则UPP>LOW,想知道这是为什么?看toUpperCase源码也没有明白。
Java
解决方案 »
- 更新时,jquery怎样根据后台设置checkbox默认选中?
- MongoDB存储字符串时的问题
- 有关中兴软创的问题,请各位前辈指教【100分求赐教。。。】
- 超紧急求java和vc 写得socket server通信问题。没分了,谁帮帮忙啊
- 正则表达式问题
- 使用 hibernate 分页查询的效率疑问
- 请问怎么在自己写的web service加上验证,不让任何人都能访问
- java 问题 (看看无妨)!!
- 大家帮忙出现Struts文件编译的奇怪的异常??
- hql 查询复合/联合 主键的表 返回的对象 少了一个值,怎么办!
- org.hibernate.MappingNotFoundException: resource: net.itaem.bean/HomeNews.hbm.xm
- 从.Net转Java,恳请各位给点建议
那为啥Aa.toUpperCase的时间大于aa.toUpperCase呢?
upp.append("Aa");
low.append("Aa");
}//代码块1
long Tupp = System.currentTimeMillis();
upp.toString().toUpperCase();
System.out.println("UPP:"+(System.currentTimeMillis()-Tupp));
//代码块1
//代码块2
long Tlow = System.currentTimeMillis();
low.toString().toLowerCase();
System.out.println("LOW:" + (System.currentTimeMillis() - Tlow));
//代码块2 //代码块3
long Tupp2 = System.currentTimeMillis();
upp.toString().toUpperCase();
System.out.println("UPP:" + (System.currentTimeMillis() - Tupp2));
//代码块3
这个代码你去执行下。
代码块3和1都是一样的。我执行的结果是 1的时间永远比2多。但是2和3的大小是不一定的。
我觉得原因在于1执行的时候要加载一些toUpperCase方法涉及到得类和方法。
所以1慢,但是2和3的时候加载过了。自然就快了。转换过程中涉及CPU占用,有时候执行toUpperCase时分配到得时间片足够执行,耗时少。有时候反而是转小写能够执行的更多(操作系统对线程调度,你没法控制,jvm也没办法。)
所以大小写转换方法效率几乎是一样的。
ps,C#中微软有针对转大写方法的特别优化,所以一般选择转大写后比较字符串。
StringBuffer upp = new StringBuffer(), low = new StringBuffer(); for (int i = 0; i <= 9000000; i++) {
upp.append("Aa");
low.append("aa");
}
long Tupp = System.currentTimeMillis();
upp.toString().toUpperCase();
System.out.println("UPP:" + (System.currentTimeMillis() - Tupp)); long Tlow = System.currentTimeMillis();
low.toString().toUpperCase();
System.out.println("LOW:" + (System.currentTimeMillis() - Tlow));
long Tupp2 = System.currentTimeMillis();
upp.toString().toUpperCase();
System.out.println("UPP:" + (System.currentTimeMillis() - Tupp2));
if (locale == null) {
throw new NullPointerException();
} int firstLower; /* Now check if there are any characters that need to be changed. */
scan: {
for (firstLower = 0 ; firstLower < count; ) {
int c = (int)value[offset+firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE) &&
(c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR) ||
(c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}好好看下这段代码