0

I was curious if I declare the variable outside of the loop, it should run faster, because when I declare it inside the loop, then in every loop the system needs to allocate a new integer in the memory which takes time. I ran the test, and the runtimes are: Outside: 3600ms Inside: 1300ms

It is the exact opposite, what I expected. Why is it faster when I declare it inside ?

public class DeclarationTestLoop {

 static double outer = 9500000;
 static double inner = 100;

 static void outside() {
    
    long start = System.currentTimeMillis();
    Integer value = 1;
    
    for(double i=0; i<outer; i++) {         
        for(int k=0; k<inner; k++) {
            value++;
            value--;
        }
    }
    
    long end = System.currentTimeMillis();
    
    System.out.println(end-start);
 }

 static void inside() {
    
    long start = System.currentTimeMillis();
    
    for(double i=0; i<outer; i++) {
        for(int k=0; k<inner; k++) {
            Integer value = 1;
            value++;
            value--;
        }
    }
    
    long end = System.currentTimeMillis();
    
    System.out.println(end-start);
 }  


 public static void main(String[] args) {
    
    
    System.out.println("outside");
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    outside();
    
    System.out.println("inside");
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    inside();
    

    System.out.println("toggle");
    outside();
    inside();
    outside();
    inside();
    outside();
    inside();
    outside();
    inside();
    outside();
    inside();
 }
}
Steel Goofy
  • 31
  • 1
  • 1
  • 1
    Do not use `System.currentTimeMillis()` but `System.nanoTime()` --- Are the results reproducible? --- Have you given the code a chance to get hot? --- Have you used a microbenchmark harness, e.g. [JMH](https://openjdk.java.net/projects/code-tools/jmh/)? --- Please read: [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Turing85 Oct 04 '20 at 11:20

1 Answers1

0

One element that could explain it is that for Java to found your outer variable, it has to first check if the variable is defined in its own scope. If it's not, then it will look outside, ...

Or it could be anything else, or a combination of those things. The real thing is that in Java, the JVM is quite optimized and is smart enough to detect you use a variable in a loop and that re-allocate it each time can be optimized.

In Java, you shouldn't design your code around "low level trick", because you aren't programming with a "low level languages". Maybe with C you could try those kind of tricks, but in Java it's a bit pointless and can decrease the readability/maintainability of your code for something that may be worst than better !

Jonathan Schoreels
  • 1,660
  • 1
  • 12
  • 20