12

I came across simple java program with two for loops. The question was whether these for loops will take same time to execute or first will execute faster than second .

Below is programs :

public static void main(String[] args) {

        Long t1 = System.currentTimeMillis();
        for (int i = 999; i > 0; i--) {
            System.out.println(i);
        }
        t1 = System.currentTimeMillis() - t1;
        Long t2 = System.currentTimeMillis();
        for (int j = 0; j < 999; j++) {
            System.out.println(j);
        }
        t2 = System.currentTimeMillis() - t2;

        System.out.println("for loop1 time : " + t1);
        System.out.println("for loop2 time : " + t2);
    }

After executing this I found that first for loop takes more time than second. But after swapping there location the result was same that is which ever for loop written first always takes more time than the other. I was quite surprised with result. Please anybody tell me how above program works.

Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
mahesh
  • 597
  • 11
  • 23
  • 7
    http://wikis.sun.com/display/HotSpotInternals/MicroBenchmarks, http://code.google.com/p/caliper/wiki/JavaMicrobenchmarks, http://www.ibm.com/developerworks/java/library/j-jtp02225/index.html, etc.. – BalusC Nov 17 '11 at 14:20
  • Can you provide the time results you got? – Kamil Sindi Nov 17 '11 at 14:21
  • 2
    How significant is the time difference? Is it consistent between different runs? – Vala Nov 17 '11 at 14:22
  • Thanks for you quick reply. output of this program : for loop1 time : 24 for loop2 time : 7 – mahesh Nov 17 '11 at 14:23
  • 1
    The program runs differently because benchmarking is hard. See [How do I write a correct micro-benchmark in Java?][1]. [1]: http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Matthew Farwell Nov 17 '11 at 14:24
  • 2
    An unrelated note: System.nanoTime() is more suited for time measurements than System.currentTimeMillis(). – Adam Zalcman Nov 17 '11 at 14:24
  • Maybe you should do 4 loops: backward, forward, backward, forward and print the 4 results (and don't print anything to screen until they are finished!) – helios Nov 17 '11 at 14:26

4 Answers4

13

The time taken by either loop will be dominated by I/O (i.e. printing to screen), which is highly variable. I don't think you can learn much from your example.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
6

The first loop will allocate 1000 Strings in memory while the second loop, regardsless of working forwards or not, can use the already pre-allocated objects.

Although working with System.out.println, any allocation should be neglible in comparison.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
2

Long (and other primitive wrappers) has cache (look here for LongCache class) for values -128...127. It is populated at first loop run.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • 1
    But there's no Long boxing-object use, except for t1 and t2 storage. System.out.print(long) receives the primitive (I've checked its implementation and it doesn't box it). – helios Nov 17 '11 at 14:31
1

i think, if you are going to do a real benchmark, you should run them in different threads and use a higher value (not just 1000), no IO (printing output during execution time), and not to run them sequentially, but one by one. i have an experience executing the same code a few times may takes different execution time. and in my opinion, both test won't be different.

simaremare
  • 401
  • 2
  • 10