ahc.util.timer
Class TimerExperiment

java.lang.Object
  |
  +--ahc.util.timer.TimerExperiment

public abstract class TimerExperiment
extends java.lang.Object

This class provides support for meaningful performance measurement of a piece of code.

To use this class, subclass it and implement the doIt method with the code for which the performance is to be measured. Pass the number of times that the code should be executed to the constructor. Optionally, you can also pass the number of iterations after which an intermediate result is recorded.

The name of the experiment serves to distinguish the results of several experiments that are performed in a single application.

When a piece is first executed, the VM executes it in interpreter mode which is comparatively slow. As the code is executed more and more often, the JIT compiler (or Hotspot or whatever) starts to optimize it more and more and translates it into machine code, resulting in a significant increase of speed. Using the default setting of 10 intermediate results, you can see when the execution time has stabilized (often after a couple of 1000 to 10000 calls). Performance data is usually interesting only for the time after the initial JIT compilation is finished.

The following code snippet gives an example of the usage of this class to test the performance of retrieving elements from different parts of a LinkedList:

 final int numIterations = 10000000;

 final List linkedList = new LinkedList ();
 for (int i=0; i<200; i++) {
     linkedList.add ("Hello");
 }

 System.out.println (new TimerExperiment ("LinkedList 0", numIterations) {
     public void doIt () {
         linkedList.get (0);
     }
 }.execute());
 System.out.println (new TimerExperiment ("LinkedList 99", numIterations) {
     public void doIt () {
         linkedList.get (99);
     }
 }.execute());
 System.out.println (new TimerExperiment ("LinkedList 199", numIterations) {
     public void doIt () {
         linkedList.get (199);
     }
 }.execute());
 

Author:
Arno Haase

Constructor Summary
TimerExperiment(java.lang.String name, int numMeasured)
           
TimerExperiment(java.lang.String name, int numMeasured, int lapTimeInterval)
           
 
Method Summary
abstract  void doIt()
           
 StopWatch execute()
          performs the actual iteration and returns a StopWatch instance with the results.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TimerExperiment

public TimerExperiment(java.lang.String name,
                       int numMeasured)

TimerExperiment

public TimerExperiment(java.lang.String name,
                       int numMeasured,
                       int lapTimeInterval)
Method Detail

execute

public StopWatch execute()
performs the actual iteration and returns a StopWatch instance with the results.


doIt

public abstract void doIt()