我在下面测试 sting.split。
import java.io.File;
public class TestSplit3 {
private static final String PROCFS = "/proc/";
public static void main(String[] args) {
//split();
testfile(Integer.parseInt(args[0]) > 0);
split();
}
private static void testfile(Boolean flag) {
long start = System.currentTimeMillis();
if (flag) {
for (int i = 0; i < 1000; i++) {
new File(PROCFS + i);
}
}
System.out.println("newfile:" + (System.currentTimeMillis() - start));
}
public static void split() {
long start = System.currentTimeMillis();
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < 1000; i++) {
String str = "asas asa s asas asas asa sa sas as as as a a"
+ "asa sasa sa sa sas as as asas as as as as as as"
+ "as as a sas asdasdas dasd asda sd ada d";
str.toString().split(" ");
}
}
System.out.println("split:" + (System.currentTimeMillis() - start));
}
}
和测试结果: [mapred@r03c02038 更长]$ ~/opt/taobao/install/jdk-1.7.0_10/bin/java TestSplit3 0 新文件:0 分裂:1772 [mapred@r03c02038 更长]$ ~/opt/taobao/install/jdk-1.7.0_10/bin/java TestSplit3 1 新文件:6 拆分:1763 [mapred@r03c02038 更长]$ ~/jdk-1.6.0_32/bin/java TestSplit3 0 新文件:0 拆分:2833 [mapred@r03c02038 更长]$ [mapred@r03c02038 更长]$ ~/jdk-1.6.0_32/bin/java TestSplit3 1 新文件:5 拆分:3416
e,在jdk7中,运行时间是一致的。但是在jdk6中,'TestSplit3 0'比'TestSplit3 1'快。谁能告诉我为什么?以及如何在jdk6中改进