-1
public class withloop {
    public static void main(String [] args) {
        long start = System.currentTimeMillis();
        int a [] = new int [5];
        
        for (int i=0; i < a.length; i++) {
            a[i] = i + 1;
        }
        
        for (int j = 0; j < a.length; j++) {
            System.out.print(a[j] + " ");
        }
        long end = System.currentTimeMillis();
        
        System.out.println(end - start);
    }

The is a simple code from my exercise to generate the runtime. After running it a few times, the runtime was suppose to change however, it did not and it stays the same no matter how many times I run it again. The output of the runtime is 0 millisecond.

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Nic Lim
  • 9
  • 3
  • 4
    Your program runs really fast. That's usually a good thing. If you want it to take longer, write something that takes longer to run – khelwood May 10 '21 at 07:37
  • Can you try to increase the size of `a` ? Displaying 10 lines probably takes < 1ms. – Arnaud Denoyelle May 10 '21 at 07:38
  • increase size of array to 500, you will see time difference – sanjeevRm May 10 '21 at 07:41
  • 1
    @ArnaudDenoyelle It's not displaying 10 lines. It's using `print` instead of `println`. `print` doesn't even flush every call. – khelwood May 10 '21 at 07:41
  • Also, as you call `print`, you probably don't print anything but just add characters to the buffer. I think that you don't "really" print until you call the last `println`. That would explain why it takes so few time. – Arnaud Denoyelle May 10 '21 at 07:41
  • 1
    *"the runtime was suppose to change"* Why?? If you don't change anything in the code and/or data, why did you expect the runtime to change? – Andreas May 10 '21 at 07:58

1 Answers1

0

Yes, output of the runtime difference is 0 millisecond, but it is correct value.

Your array has only 10 elements, you should try to use 10k elements or 100k elements, to see any delay, because 1 millisecond is too much time for such small amount of elements.

sanjeevRm
  • 1,541
  • 2
  • 13
  • 24
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59