Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Variable Declaration: Member, Outside or Inside Loop

DZone's Guide to

Variable Declaration: Member, Outside or Inside Loop

· Performance Zone
Free Resource

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, brought to you in partnership with BMC.

 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.



Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Ronald Daniel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}