今天碰到的一个有点奇怪的问题,涉及到我的知识盲区。 = , =
首先我们公司做的项目要求每个人的接口性能控制在300毫秒以内,当时在优化的过程中发现一个小问题,以下有两段简单代码:
public void test(JSONObject json) {
String str = json.getString("key");
String newStr = myService.method01(str);
Long startTime = System.currentTimeMillis();
myService.method02(newStr);//参数不同
Long endTime = System.currentTimeMillis();
System.out.println("时间1:" + (endTime - startTime));
}
public void test(JSONObject json) {
String str = json.getString("key");
String newStr = myService.method01(str);
Long startTime = System.currentTimeMillis();
myService.method02(str); //参数不同
Long endTime = System.currentTimeMillis();
System.out.println("时间2:" + (endTime - startTime));
}
当时没有测上很多次,但是能看出来 时间2 比 时间1 快了将近100毫秒,请问这之间的差距是因为什么原因引起的呢?
估计是冷启动或是缓存造成的差别,建议交叉执行各测试1000遍,取统计平均数看看,一次的偶然结果的比较没有太大意义。