home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / src / benchmark.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  2.4 KB  |  117 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. import java.util.Date;
  7.  
  8. /**
  9.  * A utility class for performing benchmarks.
  10.  * <p>
  11.  * @version 1.1
  12.  * @author ObjectSpace, Inc.
  13.  */
  14.  
  15. public class Benchmark
  16.   {
  17.   long begin;
  18.   long total;
  19.   String title;
  20.   int count;
  21.   int cycle = 100;
  22.  
  23.   /**
  24.    * Construct a benchmark with the specified title that displays its status
  25.    * automatically after every specified number of start/stop cycles.
  26.    * @param string The title
  27.    * @param n The number of cycles
  28.    */
  29.   public Benchmark( String string, int n )
  30.     {
  31.     title = string;
  32.     cycle = n;
  33.     }
  34.  
  35.   /**
  36.    * Construct a benchmark with the specified title that never displays its status
  37.    * automatically.
  38.    */
  39.   public Benchmark( String string )
  40.     {
  41.     this( string, 0 );
  42.     }
  43.  
  44.   /**
  45.    * Construct a benchmark with the title <untitled> that never displays its status  
  46.    * automatically.
  47.    */
  48.   public Benchmark()
  49.     {
  50.     this( "<untitled>", 0 );
  51.     }
  52.  
  53.   /**
  54.    * Start/resume the benchmark clock.
  55.    */
  56.   public void start()
  57.     {
  58.     begin = (new Date()).getTime(); // Current time.
  59.     }
  60.  
  61.   /**
  62.    * Stop the benchmark clock.
  63.    */
  64.   public void stop()
  65.     {
  66.     total += ((new Date()).getTime() - begin);
  67.  
  68.     if( count > 0 && cycle > 0 && count % cycle == 0 )
  69.       System.out.println( this );
  70.  
  71.     ++count;
  72.     }
  73.  
  74.   /**
  75.    * Return the current number of milliseconds on the benchmark clock.
  76.    */
  77.   public long getMilliseconds()
  78.     {
  79.     return total;
  80.     }
  81.  
  82.   /**
  83.    * Return my title.
  84.    */
  85.   public String getTitle()
  86.     {
  87.     return title;
  88.     }
  89.  
  90.   /**
  91.    * Return the number of times I've been started/restarted.
  92.    */
  93.   public int getCount()
  94.     {
  95.     return count;
  96.     }
  97.  
  98.   /**
  99.    * Return a string that describes me.
  100.    */
  101.   public String toString()
  102.     {
  103.     return "Benchmark( " + getTitle() + " x " + count + ": " + total + " ms )";
  104.     }
  105.  
  106.   /**
  107.    * Display a string that compares me with another benchmark to
  108.    * System.out.
  109.    * @param benchmark The benchmark to compare myself against.
  110.    */
  111.   public void compareTo( Benchmark benchmark )
  112.     {
  113.     float ratio = ((float) total) / ((float) benchmark.total);
  114.     System.out.println( "ratio of " + getTitle() + " to " + benchmark.getTitle() + " is " + ratio );
  115.     }
  116.   }
  117.