我在下面测试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));
}
}
and test result: [mapred@r03c02038 longer]$ ~/opt/taobao/install/jdk-1.7.0_10/bin/java TestSplit3 0 newfile:0 split:1772 [mapred@r03c02038 longer]$ ~/opt/taobao/install/jdk-1.7.0_10/bin/java TestSplit3 1 newfile:6 split:1763 [mapred@r03c02038 longer]$ ~/jdk-1.6.0_32/bin/java TestSplit3 0 newfile:0 split:2833 [mapred@r03c02038 longer]$ [mapred@r03c02038 longer]$ ~/jdk-1.6.0_32/bin/java TestSplit3 1 newfile:5 split:3416
e,在jdk7中,运行时间是一致的。但在jdk6中,'TestSplit3 0'比'TestSplit3 1'快。 谁能告诉我为什么?以及如何在jdk6中改进
答案 0 :(得分:1)
这很可能是因为您的基准测试存在缺陷。
Java中的基准测试需要考虑到开始执行程序时出现的“JVM热身”效应:
这些影响意味着您从基准测试循环获得的初始结果可能是异常的。你的代码只需要一次测量,而且没有办法判断“预热”效果是否会扭曲它。
参考: