⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multitimecostcounter.java

📁 用于求解TSP(Traveling salesman problem
💻 JAVA
字号:
/**
 * Description: provide an utility for counting multi time cost
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    Jun 07, 2005
 */

package Global.util;

import java.util.*;

public class MultiTimeCostCounter {
  private static final String DEFAULT_KEY = "DefaultKey";
  private Hashtable costCounterTable = new Hashtable();
  private boolean isCPUTime = false;

  public MultiTimeCostCounter(){}

  public MultiTimeCostCounter(boolean isCPUTime){
    this.isCPUTime = isCPUTime;
  }

  public boolean removeCounter(String countKey) {
    Object counterObj = costCounterTable.get(countKey);
    if (counterObj==null) return false;
    costCounterTable.remove(counterObj);
    return true;
  }

  public TimeCostCounter getCounter(String countKey) {
    TimeCostCounter counter = null;
    Object counterObj = costCounterTable.get(countKey);
    if (counterObj!=null) {
      counter = (TimeCostCounter)counterObj;
    } else {
      counter = new TimeCostCounter(isCPUTime);
      costCounterTable.put(countKey, counter);
    }
    return counter;
  }

  public void setStart() {
    setStart(DEFAULT_KEY);
  }

  public void setStart(String countKey) {
    getCounter(countKey).setStart();
  }

  public void setEnd() {
    setEnd(DEFAULT_KEY);
  }
  public void setEnd(String countKey) {
    getCounter(countKey).setEnd();
  }

  public long getTotalTime() {
    return getTotalTime(DEFAULT_KEY);
  }
  public long getTotalTime(String countKey) {
    TimeCostCounter counter = getCounter(countKey);
    return counter.getTotalTime();
  }

  public double getTotalTimeInSeconds() {
    return getTotalTimeInSeconds(DEFAULT_KEY);
  }

  public double getTotalTimeInSeconds(String countKey) {
    TimeCostCounter counter = getCounter(countKey);
    return counter.getTotalTimeInSeconds();
  }

  public void reset() {
    costCounterTable.clear();
  }

  public void printSelf() {
    Enumeration counters = costCounterTable.keys();
    System.out.println("@@@ TimeCostCounter @@@");
    while(counters.hasMoreElements()) {
      String key = counters.nextElement().toString();
      System.out.println("->"+key+": "+ getTotalTimeInSeconds(key));
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -