while-else 循环 [英] while-else-loop

查看:25
本文介绍了while-else 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,这在 Java 中是一个不可能的语句(迄今为止),但理想情况下我想实现它,因为它是许多迭代的核心.例如,当它创建 ArrayList 时,它被调用了 650,000 多次.不幸的是,我的实际代码在 else 循环中没有 set ;因此它将传递 addset 命令并浪费时间.

Of course this is an impossible statement in java (to-date), however ideally I would like to implement it as it is at the heart of many iterations. For example the first multiple times it is called I'm doing it 650,000+ times when it is creating the ArrayList. Unfortunately the reality is that my actual code does not have the set inside the else loop; thus it will pass over both the add and then the set commands and wasting time.

在那之后,我也在另一个循环中使用它,它只在数据已经创建时执行集合,并且它与许多其他数据嵌套在一起,因此这是一个漫长的过程.

After that I have it also in another loop where it is only performing the set as the data is already created and this is multi-nested with in many others so it is a lengthy process.

ArrayList<Integer>  dataColLinker = new java.util.ArrayList<Integer>();
...
...
public void setLinkerAt( int value, int rowIndex) {
    ...
    while(rowIndex >= dataColLinker.size()) {
        dataColLinker.add(value);
    } else {
        dataColLinker.set(rowIndex, value);
    }

有什么想法或理论吗?当涉及到 if 语句和 ArrayList 命令等等时,我不确定 java 中的速度

Any ideas or theories? I'm unsure about speeds in java when it comes to if statements and ArrayList commands and so on

推荐答案

我是否遗漏了什么?

这不是假设的代码

while(rowIndex >= dataColLinker.size()) {
    dataColLinker.add(value);
} else {
    dataColLinker.set(rowIndex, value);
}

和这个意思一样吗?

while(rowIndex >= dataColLinker.size()) {
    dataColLinker.add(value);
}
dataColLinker.set(rowIndex, value);

还是这个?

if (rowIndex >= dataColLinker.size()) {
    do {
        dataColLinker.add(value);
    } while(rowIndex >= dataColLinker.size());
} else {
    dataColLinker.set(rowIndex, value);
}

(后者更有意义......我猜).无论哪种方式,很明显您都可以重写循环,这样else 测试"就不会在循环内重复......就像我刚刚所做的那样.

(The latter makes more sense ... I guess). Either way, it is obvious that you can rewrite the loop so that the "else test" is not repeated inside the loop ... as I have just done.

FWIW,这很可能是过早优化的情况.也就是说,您可能是在浪费时间优化不需要优化的代码:

FWIW, this is most likely a case of premature optimization. That is, you are probably wasting your time optimizing code that doesn't need to be optimized:

  • 众所周知,JIT 编译器的优化器可能已经移动了代码,以便else"部分不再处于循环中.

  • For all you know, the JIT compiler's optimizer may have already moved the code around so that the "else" part is no longer in the loop.

即使没有,您尝试优化的特定事物也有可能不是重大瓶颈……即使它可能被执行 600,000 次.

Even if it hasn't, the chances are that the particular thing you are trying to optimize is not a significant bottleneck ... even if it might be executed 600,000 times.

我的建议是暂时忘记这个问题.使程序运行.当它工作时,决定它是否运行得足够快.如果没有,则对其进行分析,并使用分析器输出来确定值得花时间优化的地方.

My advice is to forget this problem for now. Get the program working. When it is working, decide if it runs fast enough. If it doesn't then profile it, and use the profiler output to decide where it is worth spending your time optimizing.

这篇关于while-else 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆