0

I was trying to check the time of execution with similar blocks.

Sample code and output are below,

    public class Tester {

    public static void main(String[] args) {
        System.out.println("Run 1");
        List<Integer> list = new ArrayList<>();
        int i = 0;
        long st = System.currentTimeMillis();
        while (++i < 10000) {
            list.add(i);
        }
        System.out.println("Time taken :" + (System.currentTimeMillis() - st));

        System.out.println("Run 2");
        int j = 0;
        List<Integer> list2 = new ArrayList<>();
        long ST = System.currentTimeMillis();
        while (++j < 10000) {
            list2.add(j);
        }
        System.out.println("Time taken :" + (System.currentTimeMillis() - ST));

        System.out.println("Run 3");
        int k = 0;
        List<Integer> list3 = new ArrayList<>();
        long ST2 = System.currentTimeMillis();
        while (++k < 10000) {
            list3.add(k);
        }
        System.out.println("Time taken :" + (System.currentTimeMillis() - ST2));
    }
}

Output

Run 1
Time taken :6
Run 2
Time taken :3
Run 3
Time taken :1

Why am I getting different time of execution?

Kajal
  • 709
  • 8
  • 27
  • 3
    Your answer will probably vary for each run of the program.. Why do you expect exactly same values?.. Side note : This is not the right way of benchmarking time taken. – TheLostMind Mar 30 '16 at 06:27
  • This is environment dependent, you do not have any control for execution time, this can vary for environments. Also you can get different time for each run. Ex I get following result for your code Run 1 Time taken :1 Run 2 Time taken :0 Run 3 Time taken :1 – RishiKesh Pathak Mar 30 '16 at 06:30
  • @TheLostMind I was noticed this kind of behavior, and got curious. It will be great if you explain me what environmental factors leads to this behavior. – Kajal Mar 30 '16 at 06:34
  • I wonder at why the runs are being executed in the same block of code. Initializing any program takes processing time, so it's only natural that the program gets faster as it 'warms up', especially with interpreted languages. A better test would be starting `Run 1` about 100+ separate times and averaging that – Aaron3468 Mar 30 '16 at 06:34
  • Always the first run takes greater time than the following runs. – Kajal Mar 30 '16 at 06:35
  • Always the first run takes greater time than the following runs :NO – RishiKesh Pathak Mar 30 '16 at 06:36
  • 1
    @Kajal The JIT is warming up, and optimizing your program better in subsequent runs. The first few seconds of execution are getting an idea of how your code executes and how to optimize it. If you compiled the code completely, this behaviour would not occur because the optimizations were done *before* runtime. – Aaron3468 Mar 30 '16 at 06:37
  • @Aaron3468 I am only running a loop in between, The list initialization is done before taking the start time. So how the warm up helps to increase the speed. – Kajal Mar 30 '16 at 06:39
  • 1
    Possible duplicate of [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Yassin Hajaj Mar 30 '16 at 06:39
  • @Kajal You don't seem to understand how Java runs code. It interprets your code and makes optimizations. The first few seconds it runs your code, it knows nothing about what it's supposed to do. As it executes (the second and third runs), it knows better, so it takes shortcuts. – Aaron3468 Mar 30 '16 at 06:41
  • @Kajal - *Always the first run takes greater time than the following runs.* . JIT can decompile methods as well.. So, that would mean that sometimes first run might take less time than the second (subsequent) runs. – TheLostMind Mar 30 '16 at 06:44
  • @Aaron3468 Thanks. Now I understand. But I still doubt how the java optimize a loop to increase the speed of execution. I may need to go in depth to java code optimization. – Kajal Mar 30 '16 at 06:44
  • @Kajal - Checkout `JitWatch`, it will show you what is being compiled by JIT and when. – TheLostMind Mar 30 '16 at 06:45

2 Answers2

1

This is probably to just-in-time compilation and hotspot optimizing on the array list, but you cannot be 100% sure.

Apart from that, your sample size is much too small to be significant.

mtj
  • 3,381
  • 19
  • 30
  • When i change the condition to while(variable<10000), the execution behaves same. – Kajal Mar 30 '16 at 06:50
  • 1
    Try numbers like 10000000 for more interesting results. Up to a certain point you will see execution become faster, later on it will get slower again due to memory management issues. – mtj Mar 30 '16 at 07:27
1

a) Since the java code is compiled to bytecode some optimizations are done to your code, anyway this might not have something to do with your observations

b) Each subsequent similar operation has better execution time until the jvm is "warmed up" for that operation, due to JVM lazy loading or CPU caching for example.

c) If you want to try benchmarking check out Java Microbenchmark harness (JMH)

skaoe
  • 11
  • 1