/**
* 時間を計測する。
* instanceを作るのと、継承してrunをoverwriteする使い方がある。
*/
public class StopWatch{
private Date start = null;
private int count;
private long cost;
private String name;
public StopWatch(){
this("");
}
public StopWatch(int count){
this("",count);
}
public StopWatch(String name){
this(name,1);
}
public StopWatch(String name,int count){
this.name = name;
this.count = count;
start();
}
public void start(){
start = new Date();
}
public void stop(){
Date end = new Date();
setCost(end.getTime()-start.getTime());
}
public void setCost(long cost){
this.cost = cost;
}
public long getCost(){
return this.cost;
}
public void print(){
System.out.println(toString());
}
public String toString(){
return getCostString();
}
public String getFormattedCost()
{
return format(getCost());
}
public static String format(long cost)
{
final long SEC = 1000;
final long MIN = SEC*60;
final long HOUR = MIN*60;
final long DAY = HOUR*24;
long ms = cost % SEC;
cost = cost - ms ;
long sec = cost % MIN / SEC;
cost = cost - sec;
long min = cost % HOUR / MIN;
cost = cost - min;
long hour = cost / HOUR;
return hour+":"+min+":"+sec+"."+ms;
}
public String getCostString()
{
StringBuffer buf = new StringBuffer();
if(null != name && !"".equals(name)){
buf.append(name);
}
buf.append(getCost()).append("[ms]");
return buf.toString();
}
public void run(){
}
public void measure(){
start();
for(int i=1;i<=count;i++){
run();
}
stop();
print();
}
}