实验1:
public class TestCharacter {
private static String buildString(int length) {
StringBuilder sb = new StringBuilder();
Random r =new Random();
for (int i = 0; i <length;i++ ){
for (int j = r.nextInt(10); j>0 ;j--){
sb.append((char)('a' + r.nextInt(30)));
}
sb.append(",");
}
return sb.toString();
}
public static void main(String[] args) {
String str = buildString(1000000); //1.7新特性, 1000000
long start;
long end;
System.out.println("-----------StringTokenizer start-----------");
start = System.currentTimeMillis();
StringTokenizer mb = new StringTokenizer(str);
StringBuilder sb = new StringBuilder();
while(mb.hasMoreTokens()){
sb.append(mb.nextToken());
}
end = System.currentTimeMillis();
System.out.println("StringTokenizer time use:" + (end-start));
System.out.println("-----------StringSpilt start-----------");
start = System.currentTimeMillis();
StringBuilder sb2 = new StringBuilder();
String[] strs = str.split("\\s");
for(String s: strs){
sb2.append(s);
}
end = System.currentTimeMillis();
System.out.println("StringSpilt time use:" + (end-start));
}
}
实验1输出结果:
-----------StringTokenizer start-----------
StringTokenizer time use:16
-----------StringSpilt start-----------
StringSpilt time use:47
说明:好像说明StringTokenize分隔字符串的效率要比spilt好一些是吧,但是为什么jdk不推荐使用这个类啦?在没弄懂这个问题之前,我一直喜欢用StringTokenize分隔字符串,感觉自己很酷。直到有一天看见别人写过一条评论后才知道,原来我用的方法不对,正则表达式\\s方法在java中是有bug的,如果要分隔字符串要换成\\t 就可以体现效率了
实验二:其他不变,把\\s改成\\t String[] strs = str.split("\\t");
输出结果为:-
----------StringTokenizer start-----------
StringTokenizer time use:16
-----------StringSpilt start-----------
StringSpilt time use:31
说明:好像和实验一一样,依然是StringTokenizer 效率高
实验三:把分隔符的逗号(,)改成空格
private static String buildString(int length) {
StringBuilder sb = new StringBuilder();
Random r =new Random();
for (int i = 0; i <length;i++ ){
for (int j = r.nextInt(10); j>0 ;j--){
sb.append((char)('a' + r.nextInt(30)));
}
sb.append(" "); //把分隔符【,】改成空格了
}
return sb.toString();
}
输出结果:-----------StringTokenizer start-----------
StringTokenizer time use:109
-----------StringSpilt start-----------
StringSpilt time use:32
说明:尼玛,逆天改变了,正则表达式效率反而更高,说明用split区分空格符号分隔字符串效率更高,而StringTokenizer区分【,。=】等符号分隔字符串效率更高。
至于它们底层的原因,我也不太清楚,希望大神帮我解释一下