/*\
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
* _____________________________________________________________________________________________________________________
*
* This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
* under the terms of the BSD License. The license text is appended to the end of this file.
\*/
package de.klaro.base.util;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
/**
* Stoppuhr fr Benchmark-Messungen.
*/
public class StopWatch extends Object
{
/* ______________________________________________________________________________________________________________ *\
\* Konstanten */
/* ______________________________________________________________________________________________________________ *\
\* Klassenvariablen */
/* ______________________________________________________________________________________________________________ *\
\* Instanzvariablen */
private int count$;
private int length$;
private List<Long> marks$;
private List<String> descriptions$;
private List<Integer> counts$;
private long start$;
/* ______________________________________________________________________________________________________________ *\
\* Konstruktoren */
/**
* Erzeugt eine Stoppuhr.
*/
public StopWatch()
{
super();
count$ = -1;
length$ = -1;
marks$ = null;
descriptions$ = null;
counts$ = null;
start$ = -1L;
}
/* ______________________________________________________________________________________________________________ *\
\* Instanzmethoden */
/**
* Startet die Stoppuhr.
*
* Alle zuvor gespeicherten Werte werden dabei gelscht.
*/
public void start()
{
count$ = 0;
length$ = 0;
marks$ = new ArrayList<Long>();
descriptions$ = new ArrayList<String>();
counts$ = new ArrayList<Integer>();
start$ = System.currentTimeMillis();
}
/**
* Setzt eine Markierung.
*
* Eine Markierung enthlt den Zeitpunkt seit Start der Stoppuhr bzw. seit dem Setzen der letzten Markierung.
*/
public void mark()
{
mark("",1);
}
/**
* Setzt eine Markierung.
*
* Eine Markierung enthlt den Zeitpunkt seit Start der Stoppuhr bzw. seit dem Setzen der letzten Markierung.
*
* @param description Der Name der Markierung
*/
public void mark(String description)
{
mark(description,1);
}
/**
* Setzt eine Markierung.
*
* Eine Markierung enthlt den Zeitpunkt seit Start der Stoppuhr bzw. seit dem Setzen der letzten Markierung.
*
* @param count Die Anzahl der Durchlufe, auf die sich die in der Markierung gespeicherte Zeit bezieht
*/
public void mark(int count)
{
mark("",count);
}
/**
* Setzt eine Markierung.
*
* Eine Markierung enthlt den Zeitpunkt seit Start der Stoppuhr bzw. seit dem Setzen der letzten Markierung.
*
* @param description Der Name der Markierung
* @param count Die Anzahl der Durchlufe, auf die sich die in der Markierung gespeicherte Zeit bezieht
*/
public void mark(String description,int count)
{
if (start$ > 0L)
{
count$++;
length$ = Math.max(length$,description.length());
marks$.add(System.currentTimeMillis() - start$);
descriptions$.add(description);
counts$.add(count);
start$ = System.currentTimeMillis();
}
}
/**
* Gibt alle Markierungen auf dem Standardausgabekanal aus.
*/
public void print()
{
print(System.out);
}
/**
* Gibt alle Markierungen aus.
*
* @param out Der Ausgabekanal
*/
public void print(PrintStream out)
{
length$ = Math.max(length$,4);
out.println("Num " + Strings.trim("Mark",length$,length$,' ',true) + " Total time Count Avg time");
out.println("--- " + Strings.string(length$,'-') + " ----------- ----- -----------");
for (int i = 0; i < count$; i++)
{
long t = marks$.get(i);
int n = counts$.get(i);
String num = Strings.format(i + 1,3);
String mark = Strings.trim(descriptions$.get(i),length$,length$,' ',true);
String total = Strings.format(t,8) + " ms";
String count = Strings.format(n,5);
String avg = (n == 0)? Strings.trim("-",11,11,' ',true): Strings.format(t / n,8) + " ms";
out.println(num + " " + mark + " " + total + " " + count + " " + avg);
}
out.println("--- " + Strings.string(length$,'-') + " ----------- ----- -----------");
out.println();
}
/* ______________________________________________________________________________________________________________ *\
\* Klassenmethoden */
/* ______________________________________________________________________________________________________________ *\
\* Klassen */
}
/*\
* _____________________________________________________________________________________________________________________
*
* This software is distributed under the terms of the BSD License:
*
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* _____________________________________________________________________________________________________________________
\*/