Home > Java > Variable Declaration : Member, Outside or Inside Loop

Variable Declaration : Member, Outside or Inside Loop

Well, my colleague and I, were having this discussion before. He was a bit confused with best practice (in performance matter) to declare the variable, whether it’s as a member, inside loop or outside loop.

public class InsideLoop {
	public void test() {

		for (int i = 0; i < 100000; i++) {
			int calc = (int) (i * Math.pow(i, i * 2));
			System.out.println(i);
		}

	}

	public static void main(String []args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new InsideLoop().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Inside Loop Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Inside Loop Time taken: "+ (endTime-startTime) +" ms");
	}

}

 

public class OutsideLoop {
	public void test() {
		int calc = 0;
		for (int i = 0; i < 100000; i++) {
			calc = (int) (i * Math.pow(i, i * 2));

		}

	}

	public static void main(String[] args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new OutsideLoop().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Outside Loop Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Outside Loop Time taken: " + (endTime - startTime) + " ms");
	}

}

 

public class Member {
	int calc;

	public void test() {

		for (int i = 0; i < 100000; i++) {
			calc = (int) (i * Math.pow(i, i * 2));
			System.out.println(i);
		}

	}

	public static void main(String[] args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new Member().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Member Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Member Time taken: " + (endTime - startTime) + " ms");
	}

}

And the results are:

Inside Loop Memory taken: 16044520 bytes
Inside Loop Time taken: 626 ms

Outside Loop Memory taken: 16044264 bytes
Outside Loop Time taken: 638 ms

Member Memory taken: 16044392 bytes
Member Time taken: 610 ms

As we can see, there is no significant difference with where did you place the variable – at least for this test cases. Thanks for the magic of JIT feature that Java has on it, so the optimization can be done at the runtime of the application and made this achieved.

Of course, this cant be taken generally, especially for non-primitive data type, String, Thread, etc. And depends of what operations that you do for that Object.

About these ads
Categories: Java
  1. 26 March 2013 at 01:00 | #1

    Declare local variable inside or outside a loop make no difference since javac will create space for 2 variables in the method frame. Java, do not create a newly local variable each time in the inner loop declaration. Obviously the unique difference is the scope, in the ‘inside loop declaration’ the variable in only visible in the loop.

  1. No trackbacks yet.

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

Follow

Get every new post delivered to your Inbox.

Join 724 other followers

%d bloggers like this: