同时,其他环 [英] while-else-loop

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

问题描述

当然这在java(迄今为止)中是一个不可能的陈述,但理想情况下我想实现它,因为它是许多迭代的核心。例如,第一次多次调用它在创建 ArrayList 时,我正在做650,000次。
不幸的是,现实是我的实际代码在 else 循环中没有 set ;因此,它将传递添加,然后传递设置命令并浪费时间。

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);
    }

任何想法或理论?
我不确定java中的速度,如果 if 语句和 ArrayList 命令等等

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 test......就像我刚刚完成的那样。

(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.

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

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