Platinum Partner
architects,high-perf,performance,tips and tricks

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.



Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

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

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}