安卓 Java 中,循环与语句,哪个在性能方面更好?

huangapple go评论119阅读模式
英文:

android java loops versus statements which is better for performance?

问题

I am asking this question because consider following scenario:

In my android app, if there are 10 items, then for selecting 3 items I check current range. For e.g. if current range is 3 to 5 (i.e. 3rd, 4th and 5th item) then I want to treat less than 3 i.e. 0 to 2 items differently and greater than 3 items differently and range items i.e. 3rd, 4th and 5th item differently. So I can do it in two ways:

1st approach) By using multiple for loops like:

for(int i=0;i<3;i++)
{
//treat less than 3 items in this loop
}

for(int i=3;i<5;i++)
{
//treat range items i.e. 3rd, 4th and 5th items in this loop
}

for(int i=4;i<9;i++)
{
//treat greater than 3 items in this loop
}

2nd approach) By using if statement in for loop like:

for(int i=0;i<9;i++)
{
if(i<3)
{
//treat less than 3 items
}
else if(i>3)
{
//treat greater than 3 items
}
else
{
//treat range items i.e. 3rd, 4th and 5th items
}
}

So which approach is better for performance? Using multiple for loops is better or using if statement inside one single for loop is better? What if I want to increase conditions? Does incrementing conditions will impact performance?

Edit 1:
Actually those items are RecyclerView items. If item is not present in range then I will recycle it. And one more thing, each and every single item contains WebView and one coordinator layout also; Actually I am using RecyclerView as a tab manager for my browser. And its one item represents one browsing tab.

英文:

I am asking this question because consider following scenario:

In my android app, if there are 10 items, then for selecting 3 items I check current range. For e.g. if current range is 3 to 5 (i.e. 3rd, 4th and 5th item) then I want to treat less than 3 i.e. 0 to 2 items differently and greater than 3 items differently and range items i.e. 3rd, 4th and 5th item differently. So I can do it in two ways:

1st approach) By using multiple for loops like:

for(int i=0;i&lt;3;i++)
{
  //treat less than 3 items in this loop
} 

for(int i=3;i&lt;5;i++)
{
  //treat range items i.e. 3rd, 4th and 5th items in this loop
} 

for(int i=4;i&lt;9;i++)
{
  //treat greater than 3 items in this loop
} 

2nd approach) By using if statement in for loop like:

for(int i=0;i&lt;9;i++)
{
  if(i&lt;3)
  {
    //treat less than 3 items
  }
  else if(i&gt;3)
  {
    //treat greater than 3 items 
  }
  else
  {
    //treat range items i.e. 3rd, 4th and 5th items 
  }
} 

So which approach is better for performance? Using multiple for loops is better or using if statement inside one single for loop is better? What if I want to increase conditions? Does incrementing conditions will impact performance?

Edit 1:
Actually those items are RecyclerView items. If item is not present in range then I will recycle it. And one more thing, each and every single item contains WebView and one coordinator layout also; Actually I am using RecyclerView as a tab manager for my browser. And its one item represents one browsing tab.

答案1

得分: 3

自问一下,在你的两个选择中,需要执行多少个“指令”。在第一个示例中,你有3个循环,但每个循环都在数据的“三分之一”上操作。而选项二只循环一次,但对“所有”元素执行if/else级联。

因此,从技术上讲,第一个解决方案导致执行更少的指令。

但真正的答案是:除非我们在处理数千个元素,否则这并不重要。Java性能不是源代码巧妙而来的。它源自简单的代码,只要必要,即时编译器就可以将其转化为最佳的机器代码。

此外,只有在遇到真正的问题时才需要担心性能。当然,要避免在源代码中做明显愚蠢的事情,但请记住:即使在移动设备上,现代CPU性能也相当强大。多一些循环或多一些操作几乎不会产生影响。与“3个循环”与“1个循环和3个if”相比,一个日志调用、数据库提取或调用远程服务器可能会带来千倍的“惩罚”。

因此,真正的答案是:尽量编写易于阅读和理解的代码。在你的情况下,在第一个示例中,你可以将每个for循环放在自己的方法中。这将使即时编译器有可能对方法体进行“内联”等操作。

你看,当你编写简单的代码时,你会更容易让即时编译器执行其工作。而且,如果你后来发现“性能很差”,那么“易于阅读”的代码也更容易更改。

英文:

Ask yourself how many "instructions" would need to be executed for your two options. In the first example, you have 3 loops, but each loop works on "1 third" of the data. Option two loops once, but "all" elements, and for "all" elements, it runs those if/else cascade.

Thus, technically, the first solution leads to less instructions.

But the real answer is: unless we are talking the processing of thousands of elements, it really doesn't matter. Java performance doesn't come out of clever source code. It comes out of the simple code, that the just-in-time compiler can turn into optimal machine code ... if necessary.

Beyond that: you should only worry about performance if you encounter real issues. Sure, avoid doing outright stupid things in your source code, but remember: modern CPUs, even in mobile devices are pretty powerful. A few more here or there are almost without effect. One logging call, or DB fetch, or call to a remote server might carry a thousand times more "penalty" compared to "3 loops" vs "1 loop with 3 ifs".

Thus the real answer is: try to write easy to read and understand code. In your case: in your first example, you could put each for-loop in its own method. Which would then enable the JIT to maybe do inlining of the method body, and such things.

You see, when you write simple code, you make it easier for the JIT to do its job. And: if you later find "performance sucks", then easy to read code is also easier to change.

答案2

得分: 1

代码部分不要翻译,以下是翻译好的内容:

这并不是效率的问题。

分开的循环可以确保处理所有的索引:

int i = 0;
for (; i < 3; i++) {
    // 在这个循环中处理少于3个项目
} 
for (; i < 6; i++) {
    // 在这个循环中处理范围内的项目即第3第4和第5个项目
} 
for (; i < 9; i++) {
    // 在这个循环中处理多于3个项目
}

或者:

int i = 0;
while (i < 3) {
    // 在这个循环中处理少于3个项目
    ++i;
} 
while (i < 6) {
    // 在这个循环中处理范围内的项目即第3第4和第5个项目
    ++i;
} 
while (i < 9) {
    // 在这个循环中处理多于3个项目
    ++i;
}

注意将 &lt; 5 更改为 &lt; 6


这稍微好一些,因为条件的语义/含义更加自解释。因此,更易读。

英文:

It is not a matter of efficiency.

Separate loops may ensure that all indices are treated:

int i = 0;
for (; i &lt; 3; i++) {
    // treat less than 3 items in this loop
} 
for (; i &lt; 6; i++) {
    // treat range items i.e. 3rd, 4th and 5th items in this loop
} 
for (; i &lt; 9; i++) {
    // treat greater than 3 items in this loop
}

Or:

int i = 0;
while (i &lt; 3) {
    // treat less than 3 items in this loop
    ++i;
} 
while (i &lt; 6) {
    // treat range items i.e. 3rd, 4th and 5th items in this loop
    ++i;
} 
while (i &lt; 9) {
    // treat greater than 3 items in this loop
    ++i;
}

Notice &lt; 5 changed to &lt; 6.


It's slightly better, as the semantics/meaning of the conditions, is more self-explaining. So it is more readable.

huangapple
  • 本文由 发表于 2020年9月2日 20:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63705314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定