2006-11-22 17:38:46 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*******************************************************************************/
|
|
|
|
package com.ibm.wala.util.perf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic class to time events
|
|
|
|
*/
|
|
|
|
public class Stopwatch {
|
|
|
|
|
2008-06-18 13:39:53 +00:00
|
|
|
protected int count;
|
2006-11-22 17:38:46 +00:00
|
|
|
|
2008-01-09 20:17:16 +00:00
|
|
|
/**
|
|
|
|
* elapsed time in nanoseconds
|
|
|
|
*/
|
2006-11-22 17:38:46 +00:00
|
|
|
private long elapsedTime;
|
|
|
|
|
|
|
|
private long startTime;
|
|
|
|
|
|
|
|
public Stopwatch() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void start() {
|
2008-01-09 20:17:16 +00:00
|
|
|
startTime = System.nanoTime();
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void stop() {
|
2008-01-09 20:17:16 +00:00
|
|
|
long endTime = System.nanoTime();
|
2006-11-22 17:38:46 +00:00
|
|
|
count++;
|
|
|
|
elapsedTime += (endTime - startTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return elapsed time in ms
|
|
|
|
*/
|
|
|
|
public long getElapsedMillis() {
|
2008-01-09 20:17:16 +00:00
|
|
|
return elapsedTime / 1000000;
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return number of times this stopwatch was stopped
|
|
|
|
*/
|
|
|
|
public int getCount() {
|
|
|
|
return count;
|
|
|
|
}
|
2008-06-18 13:39:53 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
sb.append("total: " + getElapsedMillis());
|
|
|
|
if (count > 0){
|
|
|
|
sb.append(", inv: " + count);
|
|
|
|
sb.append(", avg: " + getElapsedMillis()/count);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|