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

📄 timer.java

📁 该原代码是实现机器学习中条件随机场模型的Java代码
💻 JAVA
字号:
/**
 * 
 */
package lcrf.stuff;

import java.util.HashMap;

/**
 * @author Bernd Gutmann
 * 
 */
public class Timer {
    private static HashMap<String, Long> timers;

    public static void init() {
        if (Timer.timers == null) {
            Timer.timers = new HashMap<String, Long>(10);
        }
    }

    public static void startTimer(String name) {
        Timer.init();

        if (timers.containsKey(name)) {
            timers.remove(name);
        }

        timers.put(name, new Long(System.currentTimeMillis()));
    }

    public static long getDuration(String name) {
        Timer.init();
        if (timers.containsKey(name)) {
            return System.currentTimeMillis() - timers.get(name).longValue();
        }
        return 0;
    }

    public static String getDurationFormatted(String name) {
        Timer.init();
        long t = 0;
        if (timers.containsKey(name)) {
            t = System.currentTimeMillis() - timers.get(name).longValue();
        }

        int msec = (int) (t % 1000);
        t /= 1000;

        int sec = (int) (t % 60);
        t /= 60;

        String result = Integer.toString(msec);
        while (result.length() < 3) {
            result = result + "0";
        }

        if (sec == 0 && t == 0) {
            return "0," + result + " sec";
        }

        result = Integer.toString(sec) + "," + result + " sec";

        if (t == 0) {
            return result;
        }

        return Long.toString(t) + " min " + result;
    }
}

⌨️ 快捷键说明

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