home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 45 / SuperCD45.iso / talleres / java / stage4 / Timer.java < prev   
Encoding:
Java Source  |  2000-01-30  |  1.9 KB  |  76 lines

  1. /*
  2.  * @(#)Timer.java    1.0 00/01/30
  3.  * 
  4.  * Copyright (c) 2000, David Griffiths. All Rights Reserved.
  5.  * 
  6.  * This software is the proprietary information of David Griffiths.
  7.  * This source code may not be published or redistributed without the 
  8.  * express permission of the author. 
  9.  * 
  10.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  11.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  12.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  13.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  14.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  15.  * THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  */
  18.  
  19. /**
  20.  * A class than can be used for simple timing.
  21.  *
  22.  * Use this class by calling sampleStart() to initialize timing,
  23.  * start() and stop() many times across the piece of code
  24.  * you want to time and finally call sampleEnd() and sampleReport()
  25.  * to retrieve the times.
  26.  */
  27.  
  28. public class Timer {
  29.     static long startTime = 0L;
  30.     static long stopTime = 0L;
  31.  
  32.     static long sampTimeTot = 0L;
  33.     static int sampTimeCount = 0;
  34.     static long timeTot = 0L;
  35.     static int count = 0;
  36.  
  37.     public Timer() {
  38.     }    
  39.  
  40.     public static void start() {
  41.         startTime = System.currentTimeMillis();
  42.     }
  43.  
  44.     public static void stop() {
  45.         stopTime = System.currentTimeMillis();
  46.         timeTot += (stopTime - startTime);
  47.         count++;
  48.     }
  49.  
  50.     public static void sampleEnd() {
  51.         sampTimeTot = timeTot;
  52.         sampTimeCount = count;
  53.         timeTot = 0;
  54.         count = 0;
  55.         for (int i = 0; i < sampTimeCount; i++) {
  56.             Timer.start();
  57.             Timer.stop();
  58.         }
  59.         sampTimeTot -= timeTot;
  60.         timeTot = 0;
  61.         count = 0;
  62.     }
  63.  
  64.     public static void sampleStart() {
  65.         timeTot = 0;
  66.         count = 0;
  67.     }
  68.  
  69.     public static void report() {
  70.         System.out.println("Sample");
  71.         System.out.println("\t# of calls: " + sampTimeCount);
  72.         System.out.println("\ttotal time: " + sampTimeTot);
  73.         System.out.println("\tper pass: " + ((double)sampTimeTot / (double)sampTimeCount));
  74.     }
  75. }
  76.