home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-01-30 | 1.9 KB | 76 lines |
- /*
- * @(#)Timer.java 1.0 00/01/30
- *
- * Copyright (c) 2000, David Griffiths. All Rights Reserved.
- *
- * This software is the proprietary information of David Griffiths.
- * This source code may not be published or redistributed without the
- * express permission of the author.
- *
- * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
- * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- */
-
- /**
- * A class than can be used for simple timing.
- *
- * Use this class by calling sampleStart() to initialize timing,
- * start() and stop() many times across the piece of code
- * you want to time and finally call sampleEnd() and sampleReport()
- * to retrieve the times.
- */
-
- public class Timer {
- static long startTime = 0L;
- static long stopTime = 0L;
-
- static long sampTimeTot = 0L;
- static int sampTimeCount = 0;
- static long timeTot = 0L;
- static int count = 0;
-
- public Timer() {
- }
-
- public static void start() {
- startTime = System.currentTimeMillis();
- }
-
- public static void stop() {
- stopTime = System.currentTimeMillis();
- timeTot += (stopTime - startTime);
- count++;
- }
-
- public static void sampleEnd() {
- sampTimeTot = timeTot;
- sampTimeCount = count;
- timeTot = 0;
- count = 0;
- for (int i = 0; i < sampTimeCount; i++) {
- Timer.start();
- Timer.stop();
- }
- sampTimeTot -= timeTot;
- timeTot = 0;
- count = 0;
- }
-
- public static void sampleStart() {
- timeTot = 0;
- count = 0;
- }
-
- public static void report() {
- System.out.println("Sample");
- System.out.println("\t# of calls: " + sampTimeCount);
- System.out.println("\ttotal time: " + sampTimeTot);
- System.out.println("\tper pass: " + ((double)sampTimeTot / (double)sampTimeCount));
- }
- }
-