More iterations with less time?

Viewed 57

I just had nothing to do and started messing around with for loops in Java, so I wrote the following function

static int getCurrentTime() {
    return (int)System.currentTimeMillis();
}

which simply gets the time for me.

Now I have my main function:

public static void main(String[] args) {
    int s = 0;
    int beginTime = getCurrentTime();
    for (int j = 0; j < 2000000000; ++j)
        for (int i = 0; i < ####; ++i)
            s++;

    System.out.println("done in " + (getCurrentTime() - beginTime)+
                       " milliseconds!");
}

Now when I put 1 in the place of ####, I get the following output

done in 4118 milliseconds!

But when I put 3 in the place of ####, I get the following output

done in 11 milliseconds!

How is this possible? How did more iterations take less time? I'm really confused about what's happening here.

0 Answers
Related