有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自简单逻辑的java性能瓶颈?

我正在用Java编写一个模拟,今天我的性能明显下降。我过去可以在不等待的情况下完成我的时间步,但现在时间步之间有几秒钟的延迟。通过一些实验,我发现了一个特殊的行,当注释掉时,会使程序再次顺利执行。下面我的代码片段中指出了这一点:

public HashMap<String, Gene> childGenes(String parent, Axes axis, boolean daughter1){
    HashMap<String, Gene> parentGenes = this.cells.get(parent).getGenes();
    HashMap<String, Gene> childGenes = new HashMap<String, Gene>();
    switch(axis){
    case X:
        for(String s: parentGenes.keySet()){
            Gene g = parentGenes.get(s);
            Compartment comp = g.getLocation().getAP();
            /*commenting out this line increases performance*/ if((comp != Compartment.POSTERIOR && daughter1) || (comp != Compartment.ANTERIOR && !daughter1)){
                childGenes.put(g.getName(), new Gene(g.getName(), g.getState(), g.getLocation()).populateCons());
            }
        }
        break;

    //essentially identical cases for Y and Z here
    }
    return childGenes;
}

hashmap大约有30个成员,因此这个for循环将执行30次,并且这个方法将快速连续调用两次,总共运行大约60次。从计算机的角度来看,这仍然像是小豆,而且if语句并没有包含任何计算量特别大的内容,对吧但是评论这一行会疯狂地提高性能。会发生什么事

提前谢谢


共 (0) 个答案