Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a variable back to zero

Tags:

java

I'm teaching myself Java using one of the ebooks on Amazon. I was doing one of the lessons which does a "benchmark" of the computer. It does this by looping for a minute and calculating the results.

It basically doesn't show you anything for a minute until it's done. So my was making a small modification to show a dot as a progress bar of sorts every few seconds. Normally this is a trivial thing, but something's not right, and I don't know what.

What happens is miniIndex will reach the threshold I specified, and print the value of miniIndex and a period. Its then supposed to set miniIndex to zero so that counter can restart. But it doesn't reset, and never increments again. It's very odd behavior.

Here is the code in its entirety:

class Benchmark {
    public static void main(String[] arguments) {

        long startTime = System.currentTimeMillis();
        long endTime = startTime + 60000;
        long index = 0;

        // My inner index
        int miniIndex = 0;
        //
        while (true) {
            double x = Math.sqrt(index);
            long now = System.currentTimeMillis();
            if (now > endTime){
                break;
            }
            index++;
            // my modification
            miniIndex++;
            if (miniIndex >= 5000) {
                System.out.print(miniIndex + ".");
                miniIndex = 0;
            }
            // end of my modification
        }
        System.out.println(index + " loops in one minute.");
    }
}
like image 343
Steel Rat Avatar asked Aug 09 '19 19:08

Steel Rat


People also ask

How do you make a variable unchangeable?

First: Declare your final variable without an initial value. Second: Use a temporary variable to compute the unique value. Last: Load the temporary value into your final variable. If it is an instance variable, you'll have to do this computation into every constructor.

How do you reset an HTML variable?

To unset a variable in JavaScript, use the undefined. After that, use delete operator to completely remove it.


2 Answers

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).

like image 149
Nexevis Avatar answered Oct 16 '22 10:10

Nexevis


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.

like image 5
Joseph Larson Avatar answered Oct 16 '22 10:10

Joseph Larson