Over a million developers have joined DZone.
Platinum Partner

Variable Declaration: Member, Outside or Inside Loop

· Performance Zone

The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docker and containers in general to create packaged images for easy management, testing, and deployment of software.

 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.



The Performance Zone is brought to you in partnership with New Relic. Read more about providing a framework that gets you started on the right path to move your IT services to cloud computing, and give you an understanding as to why certain applications should not move to the cloud.

Topics:

Published at DZone with permission of Ronald Daniel , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}