Jump to content

Benchmark Test

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
jpconleyiv

jpconleyiv

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Hello Everyone!
This is my first post here in regards to a programming problem (I'm excited!). I have been working through Sam's Teach Yourself Java and have came across a workshop in the book that tests how fast your system is operating. This code is in the book, I have typed it out code for code, and cannot get it to run properly in NetBeans. When I ran it the first time I received '0 loops in one minute' now for some reason (nothing has changed) the program does not stop running.

If anyone can help me out with whats wrong that would be great, I'm actually learning it and not just looking for an answer so I really need to know whats going on in the code that is causing this issue.
import java.util.*;


class Benchmark {

    public static void main(String[] args) {


        Calendar start = Calendar.getInstance();


        int startMinute = start.get(Calendar.MINUTE);

        int startSecond = start.get(Calendar.SECOND);


        start.roll(Calendar.MINUTE, true);


        int nextMinute = start.get(Calendar.MINUTE);

        int nextSecond = start.get(Calendar.SECOND);


        int index = 0;


        while (true) {

            double x = Math.sqrt(index);


            GregorianCalendar now = new GregorianCalendar();

            if (now.get(Calendar.MINUTE) >= nextSecond) {


                if  (now.get(Calendar.SECOND) >= nextSecond) {


                    break;

            }

        }


        index++;

    }


        System.out.println(index + "    loops in one minute.");


    }

} 

Thanks for all of your time and for stopping in to take a look.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
Try running this code, it seems to work:
import java.util.*;

class Benchmark {
    public static void main(String[] args) {
        Calendar start = Calendar.getInstance();
        int startMinute = start.get(Calendar.MINUTE);
        int startSecond = start.get(Calendar.SECOND);
        start.roll(Calendar.MINUTE, true);
        int nextMinute = start.get(Calendar.MINUTE);
        int nextSecond = start.get(Calendar.SECOND);
        int index = 0;
        while (true) {
            double x = Math.sqrt(index);
            GregorianCalendar now = new GregorianCalendar();
            if (now.get(Calendar.MINUTE) >= nextMinute) {
                if (now.get(Calendar.SECOND) >= nextSecond) {
                    break;
                }
            }
            index++;
        }
        System.out.println(index + " loops in one minute.");
    }
}

You had

if (now.get(Calendar.MINUTE) >= nextSecond) {

Instead of

if (now.get(Calendar.MINUTE) >= nextMinute) {
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jpconleyiv

jpconleyiv

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Thanks for testing this and going through it. I appreciate it. I did make the changes and IT WORKS!!

My output: 110138899 loops in one minute.

Thank You so much!! I have been fighting with this program for days now!