Setting a variable back to zero

I think you misunderstand what your miniIndex++ operation is doing, as it is not counting milliseconds but instead is counting the number of loop iterations which are not equal to each other. I have modified your code to execute the if statement inside every 5 seconds, according to what you wanted to happen:

public static void main(String[] arguments) {

    long startTime = System.currentTimeMillis();
    long miniTime = startTime; //Declare miniTime equal to startTime
    long endTime = startTime + 60000;
    long index = 0;

    while (true) {
        double x = Math.sqrt(index);
        long now = System.currentTimeMillis();
        if (now > endTime){
            break;
        }
        index++;

        // my modification    
        //Current time minus last time the if executed and check if 5 seconds passed
        if ((now - miniTime) >= 5000) { 
            miniTime = System.currentTimeMillis();
            System.out.println("5 seconds have passed.");

            //if you want to print the actual time elapsed every 5 seconds use this print
            //System.out.println((now - startTime)/1000 + " seconds have passed.");
        }
        // end of my modification
    }
    System.out.println(index + " loops in one minute.");
}

Notice how I now compare current time of now and subtract the miniTime to check to see if it is higher than or equal 5000 milliseconds. To use time you must relate it to time somehow, in this case System.currentTimeMillis() and the results. Numbers themselves such as counting the loop will never be time consistent.

A loop may execute millions of times, but only take 3 seconds.

Example Output:

5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
5 seconds have passed.
16319642816 loops in one minute.

Note: 5 seconds have passed. prints 11 times because at the 60 second mark the loop is broken so the final pass is not printed. (And 11 * 5 is 55 for the first 55 seconds).


Your code is working fine; it's your expectations that are flawed.

Your code prints a dot every 5,000 iterations. Which is going to basically spew values. Remember, your CPU is running at something > 2 billion operations a second. You can probably do a few million of those loops per second. Call it a million loops per second, divided by 5000 is 200 dots per second, more or less, anyway.


If you still want to use it in that type of output you can make it easier using StringBuilder. It's coded like that:

StringBuilder stringBuilder = new StringBuilder();

and incide the loop like that:

if (miniIndex >= 5000) {
    stringBuilder.append(miniIndex).append(".");
    miniIndex = 0;
}

if (stringBuilder.length() >= 200) {
    System.out.println(stringBuilder);
    stringBuilder.setLength(0);
}

enter image description here

Tags:

Java