Monday, August 30, 2010

Avoid Nested Loop

Nested loop gives big impact on performance and some time it make worse if not controlled properly. Need to double think before using the nested loop and try to find some alternative way to handle the same. Even if it is used, it should not go to level three in nested loop.
It is explained in the below code.

public class TestLoop {
public static void main(String s[]) {
long start, end;
int[] a = new int[100000];
int[] b = new int[100000];
start = System.currentTimeMillis();
for (int i = 0; i <>
}
end = System.currentTimeMillis();
System.out.println(end - start + " " + "milli seconds for One Time Loop");
start = System.currentTimeMillis();
for (int j = 0; j <>
for (int i = 0; i <>
}
end = System.currentTimeMillis();
System.out.println(end - start + " " + "milli seconds for loop B inside Loop A");
}
}
Result:
0 milli seconds for One Time Loop
44015 milli seconds for loop B inside Loop A
You can see the above result how it becomes 44015 ms for executing the loop inside the loop. These are figure for just an empty loop. For any process getting invoked inside the nested loop, the time will be exorbitantly increase.. I would suggest you to think before using the nesting loop with large data and try to avoid as much as possible. You could try to change/write a code in such a way that it would minimize the nested loop. It is explained using the below code. In the below code I am trying to match the value in a big array and print the ‘Found match’ on the console.
public class TestLoop {
public static void main(String s[]) {
long start, end; String[] arrFirst = new String[1000000];
String[] arrSecond = { "0", "1" };
for (int i = 0; i <>length; i++) {
arrFirst[i] = String.valueOf(i);
}
start = System.currentTimeMillis();
for (int i = 0; i <>length; i++) {
for (int j = 0; j <>length; j++) {
if (arrFirst[i].equalsIgnoreCase(arrSecond[j])) {
System.out.println("Found match");
}
}
}
end = System.currentTimeMillis();
System.out.println(end - start + " milli "+ "seconds for ");
start = System.currentTimeMillis();
}
}
Result: 30 ms
As you can see that it iterate the 1000000*2 times to complete the process. Since we know arrSecond having only two values 0 and 1 therefore we could easily minimize the code using below way

public class TestLoop1 {
public static void main(String s[]) {
long start, end;
String[] arrFirst = new String[1000000];
String[] arrSecond = { "0", "1" };
for (int i = 0; i <>length; i++) {
arrFirst[i] = String.valueOf(i);
}
start = System.currentTimeMillis();
for (int i = 0; i <>length; i++) {
if ((arrSecond[0].equalsIgnoreCase(arrFirst[i]))|| (arrSecond[1].equalsIgnoreCase(arrFirst[i]))) {
System.out.println("Found match");
}
}
end = System.currentTimeMillis();
System.out.println(end - start + " milli "+ "seconds for loop");
}
}

Result: 15 ms

You could see it just iterate 1000000 times just half the above code and performance get improved twice.
I agree that this is not the best example but this would definitely provide you the way how to handle the nested loop

No comments:

Post a Comment