何时合理的时间来做第4行而不是第3行?或者它们是完全冗余的调用?
1 Object o1 = new Object();
2
3 int hcObj = o1.hashCode();
4 int hcSys = System.identityHashCode(o1);
Run Code Online (Sandbox Code Playgroud) 我们进行了附加测试.结果一致表明,通过索引访问数组比通过密钥访问地图快10倍.这个数量级的差异让我们感到惊讶.
我们对Map的关键是java.lang.String ...是计算Map键的java.lang.String.hashcode()实现的独家原因吗?在附加的代码中,我只用了一把钥匙
java.lang.String key = 1;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,编译器/运行时缓存不是吗?或者它会在每次调用时重新计算?
感谢您的任何见解.
public class PerfTest {
static java.util.HashMap<String, Double> map;
static Double[] array = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
static long nTimes = 1000000;
static{
map = new java.util.HashMap<String, Double>();
map.put("1", new Double(1));
map.put("2", new Double(2));
map.put("3", new Double(3));
map.put("4", new Double(4));
map.put("5", new Double(5));
map.put("6", new Double(6));
map.put("7", new Double(7));
map.put("8", new Double(8));
map.put("9", new Double(9));
map.put("10", new Double(10));
}
public static void main(String[] args){
PerfTest tester = new PerfTest();
long timeInMap = tester.testHashMap();
long timeInArray …
Run Code Online (Sandbox Code Playgroud)