Testing Your Computer Speed

This hour’s workshop is a Java program that performs a benchmark, a test that measures how fast computer hardware or software is operating. The Benchmark program uses a loop statement to repeatedly perform the following mathematical expression:

double x = Math.sqrt(index);

This statement calls the Math.sqrt() method to find the square root of a number. You learn how methods work during Hour 11, “Describing What Your Object Is Like.”

The benchmark you’re creating sees how many times a Java program can calculate a square root in one minute.

Use NetBeans to create a new empty Java file called Benchmark. Enter the text of Listing 8.2 and save the program when you’re done.

Listing 8.2. The Full Source Code of Benchmark.java


 1: class Benchmark {
 2:     public static void main(String[] arguments) {
 3:         long startTime = System.currentTimeMillis();
 4:         long endTime = startTime + 60000;
 5:         long index = 0;
 6:         while (true) {
 7:             double x = Math.sqrt(index);
 8:             long now = System.currentTimeMillis();
 9:             if (now > endTime) {
10:                 break;
11:             }
12:             index++;
13:         }
14:         System.out.println(index + " loops in one minute.");
15:     }
16: }


The following things take place in the program:

• Lines 1–2: The Benchmark class is declared and the main() block of the program begins.

• Line 3: The startTime variable is created with the current time in milliseconds as its value, measured by calling the currentTimeMillis() method of Java’s System class.

• Line 4: The endTime variable is created with a value 60,000 higher than startTime. Because one minute equals 60,000 milliseconds, this sets the variable one minute past startTime.

• Line 5: A long named index is set up with an initial value of 0.

• Line 6: The while statement begins a loop using true as the conditional, which causes the loop to continue forever (in other words, until something else stops it).

• Line 7: The square root of index is calculated and stored in the x variable.

• Line 8: Using currentTimeMillis(), the now variable is created with the current time.

• Lines 9–11: If now is greater than endTime, this signifies that the loop has been running for one minute and break ends the while loop. Otherwise, it keeps looping.

• Line 12: The index variable is incremented by 1 with each trip through the loop.

• Lines 14: Outside the loop, the program displays the number of times it performed the square root calculation.

The output of the application is shown in the Output pane in Figure 8.1.

Figure 8.1. The output of the Benchmark program.

Image

The Benchmark program is an excellent way to see whether your computer is faster than mine. During the testing of this program, my computer performed around 4.5 billion calculations. If your computer has better results, don’t just send me your condolences. Buy more of my books so I can upgrade.

Summary

Loops are a fundamental part of most programming languages. Animation created by displaying several graphics in sequence is one of many tasks you could not accomplish in Java or any other programming language without loops.

Every one of Bart Simpson’s chalkboard punishments has been documented on the Web. Visit www.snpp.com/guides/chalkboard.openings.html to see the list along with a Java program that runs on the page drawing Bart’s sayings on a green chalkboard.

Q&A

Q. The term initialization has been used in several places. What does it mean?

A. It means to give something an initial value and set it up. When you create a variable and assign a starting value to it, you are initializing the variable.

Q. If a loop never ends, how does the program stop running?

A. Usually in a program where a loop does not end, something else in the program is set up to stop execution in some way. For example, a loop in a game program could continue indefinitely while the player still has lives left.

One bug that crops up often as you work on programs is an infinite loop, a loop that never stops because of a programming mistake. If one of the Java programs you run becomes stuck in an infinite loop, press the red alert icon to the left of the Output pane.

Q. How can I buy stock in the Green Bay Packers?

A. Unless the publicly owned NFL team decides to hold another stock sale, the only way to become a stockholder is to inherit shares in a will.

The Packers have sold stock in 1923, 1935, 1950, and 1997. Approximately 112,000 people own 4.7 million shares in the team, despite the fact that they have very limited rights associated with the stock.

Holders don’t earn a dividend and can’t profit from their shares. They only can sell them back to the team and lose money in the deal. No individual can own more than 200,000 shares.

They do receive exclusive team merchandise offers and can attend an annual meeting to elect the seven-member board that manages the team.

In the 1923 stock sale that formed the franchise, 1,000 fans bought shares for $5 each. The 1997 sale raised $24 million for the Lambeau Field renovation.

More information on the stock can be found on the Web at www.packers.com/community/shareholders.html.

Workshop

The following questions test your knowledge of loops. In the spirit of the subject matter, repeat each of these until you get them right.

Quiz

1. What must be used to separate each section of a for statement?

A. Commas

B. Semicolons

C. Off-duty police officers

2. Which statement causes a program to go back to the statement that began a loop and then keep going from there?

A. continue

B. next

C. skip

3. Which loop statement in Java always runs at least once?

A. for

B. while

C. do-while

Answers

1. B. Commas are used to separate things within a section, but semicolons separate sections.

2. A. The break statement ends a loop entirely, and continue skips to the next go-round of the loop.

3. C. The do-while conditional isn’t evaluated until after the first pass through the loop.

Activities

If your head isn’t going in circles from all this looping, review the topics of this hour with the following activities:

• Modify the Benchmark program to test the execution of simple mathematical calculation such as multiplication or division.

• Write a short program using loops that finds the first 400 numbers that are multiples of 13.

To see Java programs that implement these activities, visit the book’s website at www.java24hours.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.239.103