Chaitanya Karmarkar
Chaitanya Karmarkar

Reputation: 1595

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.

Upvotes: 2

Views: 152

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109623

It is not a matter of efficiency.

Separate loops may ensure that all indices are treated:

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

Or:

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

Notice < 5 changed to < 6.


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

Upvotes: 1

GhostCat
GhostCat

Reputation: 140641

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.

Upvotes: 3

Related Questions