 Let's look at the code that we used for timing the sum from 1 to N method. There are three main parts. We get the current time, then do the method that we want to examine, and figure out the elapsed time. All of this is in a loop that goes for as many trials as we want. The loop and timing is the same for every timing test we want to do. The only thing that changes is the code that we want to time. In order to eliminate the need for duplicate code, we've constructed a class that helps us do timed tests. The class will have the number of times you want to run the test and the number that you want to keep. By default, we'll run each test 25 times and keep the last five timings. Remember, we're doing this because the Java virtual machine needs time to stabilize for its setup and for its memory usage. If you're not happy with the default values, you can use this other constructor to change the number of trials to run and the number of results to keep to anything you want. And here are the getters and setters. Nothing new to see here. Those are followed by three methods that you can override. The first one is setup. Some tests may need to set up variables or files. And you can do this by overriding the setup method. The do test method will do the actual test that we want to time. You'll definitely want to do something in this method when you overwrite it. After each trial, you may need to get rid of some of the objects you created during setup. That's what teardown does. For example, if your setup method created a file, your teardown method might delete that file if it's no longer needed. This method is where the magic happens in run test. It starts by declaring an array to save the timings we want to keep and an index into that array. Here's our for loop. Inside the loop, we do our setup and then call the system garbage collector method. This method will clear out any unused memory. We get the current JVM time. And at last, we do our test. After the test, we get our elapsed time. If this is one of the trials that we want to keep, we store the elapsed time in the results array and move to the next index. And then do any teardown that's necessary before we go to our next trial. When all of the trials are finished, we return the array of timings to whoever called us. The last method in our class returns the average for the tests that we wanted to keep. It calls run test to gather those times, and then calculates the average to return to the caller. Now that we have our framework, how do we put it to use? Let's look at that first anagram method from section 2.4.1 in the book. The book says it's supposed to be a quadratic algorithm, so let's do some timing tests to see if it really does exhibit quadratic performance. We're going to try detecting anagrams with strings of length 2600, 5200, 7800, and 10400. Why those numbers? Because they're multiples of 26, and there's 26 letters in the alphabet. Let's set up the test code in the anagram test class, which extends our time test framework. We'll need to have two strings that we want to compare, and we might as well make those private. We'll want to have the number of times we're repeating the alphabet, and we'll also use constants for the alphabet and a mixed-up alphabet. I've copied and pasted them here, so you won't have to watch me typing them. Now we need the constructor. It's going to be public anagram test, and its argument is going to be the number of repetitions of the alphabet that we want to use for our anagram. We'll call the superclass constructor, which gives us 25 trials, keeping the last 5, and then we'll set our variable for the number of repetitions. In this case, we do need to override the setup method. That's where we're going to construct the strings to check for whether they're anagrams or not. So here's our public void setup. We'll set S1 to the empty string, and S2 to the empty string, and then run a for loop from 0 up to the number of repetitions of the alphabets, and we'll tack on the alphabet to S1 and the mixed alphabet to S2. I left off this dot here because it's unambiguous, but just for the sake of completeness, let's put that in here. We'll also need to override the do-test method, which is going to do the actual checking for whether the S1 and S2 are anagrams or not. Again, I'm going to copy and paste this so you don't have to watch me type all of it, and there's the code. That's all we need to do for this class that extends the time-test framework. We don't need any teardown here. We'll reallocate the memory as we need it during setup. Now let's return to main and make use of what we just did here. We're going to create an array that tells how many copies of the alphabet we want to work with. We'll call it alpha copies, and we'll start with 100 repetitions, 200, 300, and 400 repetitions of the alphabet. Now set up a loop for each of the copies in alpha copies. We'll create an instance of our anagram test, and it'll be a new anagram test with the number of copies of the alphabet we want. We'll run the test and get the average time for that many copies. Our average time becomes test.getAverageTestTime, and then we'll print the result. Using printf, so with our length and the time, that'll be copies times 26, and our average time. Let's save that, and hope I've typed everything correctly. Let's compile it. It says the compilation finished successfully, and now let's run it and see how this algorithm performs. So when I doubled the length from 2600 to 2200, I got 4 times as much time, 3 times as long, took about 9 times as long, and 4 times as long took about 16 times as long. So yes, this version of anagram detection is order n squared is a quadratic algorithm. So again, here's how to use our time test framework. In the time test framework, you create any variables that you might need. Make a constructor to set the parameters the way you like them. If you need a setup, overwrite it and put it in. You'll always want something to override due test, because this is where you're going to actually be testing things. If you need a tear down, you can also put that in there. And then in your main, you create a new instance of this test that you just wrote, and get its average test time, and save it in a variable where you can print it out later.