Calling object inside the loop

We could use to create the object inside or outside of the loop depends upon the behavior of the class.
If any class having class member variable then class needs to be inside the loop and that will limit their scope which is good to garbage collect but if class used as Utility class and doesn’t have member class variable then we could declare the class out side of the loop and used inside the loop.
Below example shows there are only 32 ms would take to instantiate the object inside the loop but best thing is that it would limit the scope which is good during garbage collection

public class TestLoop {
public static void main(String s[]) {
long start, end;
int[] a = new int[1000000];
start = System.currentTimeMillis();
TestName tname1 = new TestName();
for (int i = 0; i
// tname1=new TestName();
}
end = System.currentTimeMillis();
System.out.println(end – start + ” milli “
+ “seconds for One Time Loop”);
start = System.currentTimeMillis();
for (int j = 0; j
TestName tname = new TestName();
}
end = System.currentTimeMillis();
System.out.println(end – start + ” “
+ “milli seconds loop calling object inside Loop “);
}
}
Result: 0 milli seconds for One Time Loop
32 milli seconds for loop calling object inside Loop

As per above example you could see the declaring the object inside loop doesn’t have much impact on performance.

In below example explain the two ways to declare the object option 1 inside declaration and option 2 out side declaration.

Option 1:
for (int i=0; i
{
Object obj = tempList.get(i);
o. doOperation();
}
Option 2:
Object o;
for (int i=0; i
{
o = tempList.get(i);
o.doOperation();
}

We could say that Option1 better as it restricts scope of ‘obj’ variable to the for block. From a performance perspective, it might not have any effects in Java, but it might have in lower level compilers. They might put the variable in a register if you do the first.
Construction of an object using new is totally different from just declaring it, of course.
I think readability is more important than performance and from a readability standpoint, the first code is definitely better.
But if you have utility class which doesn’t have member variables we could instantiate once and reuse across the code.

In the below example I am trying to explain the utility class which could be instantiate once and use across the application.

import java.util.Calendar;
import java.util.Date;
public class TestLoop {
public static void main(String s[]) {
long start, end;
int[] a = new int[1000000];
Calendar cal = Calendar.getInstance();
start = System.currentTimeMillis();
for (int i = 0; i
Date dt = cal.getTime();
}
end = System.currentTimeMillis();
System.out.println(end – start + ” milli “
+ “seconds for Instantiating object out side Loop”);
start = System.currentTimeMillis();
TestName tname = null;
for (int j = 0; j
Date dt = Calendar.getInstance().getTime();
}
end = System.currentTimeMillis();
System.out.println(end – start + ” “
+ “milli seconds Instantiating object inside Loop “);
}
}
0 milli seconds for Instantiating object out side Loop
797 milli seconds Instantiating object inside Loop

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s