Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用两个相似的代码块在HashMap中查找密钥的运行时上存在显著差异_Java_Hashmap_Runtime - Fatal编程技术网

Java 使用两个相似的代码块在HashMap中查找密钥的运行时上存在显著差异

Java 使用两个相似的代码块在HashMap中查找密钥的运行时上存在显著差异,java,hashmap,runtime,Java,Hashmap,Runtime,我使用HashMap.containsKey检查HashMap中是否存在键,并在下面的代码中计算了持续时间: public static void main(String[] args){ long startTime = System.nanoTime(); HashMap<String,Integer> map = new HashMap<>(); map.put("one", 1); map.put("

我使用HashMap.containsKey检查HashMap中是否存在键,并在下面的代码中计算了持续时间:

public static void main(String[] args){
    long startTime = System.nanoTime();

    HashMap<String,Integer> map = new HashMap<>();
    map.put("one", 1);
    map.put("two", 2);
    map.put("three", 3);
    if(map.containsKey("four")){
        System.out.println("true");
    }

    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    System.out.println(duration);
}
public static void main(String[] args){
    long startTime = System.nanoTime();

    HashMap<String,Integer> map = new HashMap<>();
    map.put("one", 1);
    map.put("two", 2);
    map.put("three", 3);
    if(!(map.containsKey("four"))){
        System.out.println("true");
    }

    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    System.out.println(duration);
}
publicstaticvoidmain(字符串[]args){
long startTime=System.nanoTime();
HashMap=newHashMap();
地图.放("一",一);;
地图("二",二);;
地图放置(“三”,3);
如果(图四){
System.out.println(“真”);
}
long-endTime=System.nanoTime();
长持续时间=结束时间-开始时间;
系统输出打印项次(持续时间);
}
持续时间为:48278

我使用HashMap.containsKey检查HashMap中是否不存在键,并在下面的代码中计算了持续时间:

public static void main(String[] args){
    long startTime = System.nanoTime();

    HashMap<String,Integer> map = new HashMap<>();
    map.put("one", 1);
    map.put("two", 2);
    map.put("three", 3);
    if(map.containsKey("four")){
        System.out.println("true");
    }

    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    System.out.println(duration);
}
public static void main(String[] args){
    long startTime = System.nanoTime();

    HashMap<String,Integer> map = new HashMap<>();
    map.put("one", 1);
    map.put("two", 2);
    map.put("three", 3);
    if(!(map.containsKey("four"))){
        System.out.println("true");
    }

    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    System.out.println(duration);
}
publicstaticvoidmain(字符串[]args){
long startTime=System.nanoTime();
HashMap=newHashMap();
地图.放("一",一);;
地图("二",二);;
地图放置(“三”,3);
如果(!(地图包含(“四”)){
System.out.println(“真”);
}
long-endTime=System.nanoTime();
长持续时间=结束时间-开始时间;
系统输出打印项次(持续时间);
}
持续时间为:378741

因此,在第一个代码中,我检查我的地图中是否存在一个键“4”,在第二个代码中,我检查地图中是否不存在一个键(检查第7行)。
为什么运行时在第二个中如此长?它们基本上不是在做相同的事情吗?

在您的示例中,这种差异是由使用System.out.println()引起的。这需要时间,应该是主要原因

不管怎样,你的测量方法很差。执行时间可能会因时间的不同而变化很大。这是由于许多因素造成的,即JVM的JIT或CPU的缓存未命中。如果只测量一次执行时间,则结果不准确。如果你执行你的代码
几十万次,这些不一致加起来等于零。

只需删除
System.out.println(“true”)并查看diff@Eklavya非常感谢。