zoukankan      html  css  js  c++  java
  • 计时器StopWatch的使用(附完整源码)

    因为经常用到,这里做下笔记,简单总结一下StopWatch的使用,Spring提供的计时器秒表支持方便以秒和毫秒为单位计时的程序时间特性的统计输出,尤其是单线程、顺序执行的程序。也就是说,如果我们有几个按顺序执行的任务,而我们又分别关心几个任务的执行所占用的时间,我们希望形成一个不那么复杂的日志输出,秒表提供了这样的功能。而Spring的秒表基本上只针对这个功能实现。

    性急的朋友,先拿StopWatch测试源码:

    import java.text.NumberFormat;
    import java.util.LinkedList;
    import java.util.List;
    
    public class StopWatch {
        private final String id;
        private boolean keepTaskList = true;
        private final List<TaskInfo> taskList = new LinkedList();
        private long startTimeMillis;
        private boolean running;
        private String currentTaskName;
        private StopWatch.TaskInfo lastTaskInfo;
        private int taskCount;
        private long totalTimeMillis;
    
        public StopWatch() {
            this.id = "";
        }
    
        public StopWatch(String id) {
            this.id = id;
        }
    
        public void setKeepTaskList(boolean keepTaskList) {
            this.keepTaskList = keepTaskList;
        }
    
        public void start() throws IllegalStateException {
            this.start("");
        }
    
        public void start(String taskName) throws IllegalStateException {
            if (this.running) {
                throw new IllegalStateException("Can't start StopWatch: it's already running");
            } else {
                this.startTimeMillis = System.currentTimeMillis();
                this.running = true;
                this.currentTaskName = taskName;
            }
        }
    
        public void stop() throws IllegalStateException {
            if (!this.running) {
                throw new IllegalStateException("Can't stop StopWatch: it's not running");
            } else {
                long lastTime = System.currentTimeMillis() - this.startTimeMillis;
                this.totalTimeMillis += lastTime;
                this.lastTaskInfo = new StopWatch.TaskInfo(this.currentTaskName, lastTime);
                if (this.keepTaskList) {
                    this.taskList.add(this.lastTaskInfo);
                }
    
                ++this.taskCount;
                this.running = false;
                this.currentTaskName = null;
            }
        }
    
        public boolean isRunning() {
            return this.running;
        }
    
        public long getLastTaskTimeMillis() throws IllegalStateException {
            if (this.lastTaskInfo == null) {
                throw new IllegalStateException("No tasks run: can't get last task interval");
            } else {
                return this.lastTaskInfo.getTimeMillis();
            }
        }
    
        public String getLastTaskName() throws IllegalStateException {
            if (this.lastTaskInfo == null) {
                throw new IllegalStateException("No tasks run: can't get last task name");
            } else {
                return this.lastTaskInfo.getTaskName();
            }
        }
    
        public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException {
            if (this.lastTaskInfo == null) {
                throw new IllegalStateException("No tasks run: can't get last task info");
            } else {
                return this.lastTaskInfo;
            }
        }
    
        public long getTotalTimeMillis() {
            return this.totalTimeMillis;
        }
    
        public double getTotalTimeSeconds() {
            return (double) this.totalTimeMillis / 1000.0D;
        }
    
        public int getTaskCount() {
            return this.taskCount;
        }
    
        public StopWatch.TaskInfo[] getTaskInfo() {
            if (!this.keepTaskList) {
                throw new UnsupportedOperationException("Task info is not being kept!");
            } else {
                return (StopWatch.TaskInfo[]) this.taskList.toArray(new StopWatch.TaskInfo[this.taskList.size()]);
            }
        }
    
        public String shortSummary() {
            return "StopWatch '" + this.id + "': running time (millis) = " + this.getTotalTimeMillis();
        }
    
        public String prettyPrint() {
            StringBuilder sb = new StringBuilder(this.shortSummary());
            sb.append('
    ');
            if (!this.keepTaskList) {
                sb.append("No task info kept");
            } else {
                sb.append("-----------------------------------------
    ");
                sb.append("ms     %     Task name
    ");
                sb.append("-----------------------------------------
    ");
                NumberFormat nf = NumberFormat.getNumberInstance();
                nf.setMinimumIntegerDigits(5);
                nf.setGroupingUsed(false);
                NumberFormat pf = NumberFormat.getPercentInstance();
                pf.setMinimumIntegerDigits(3);
                pf.setGroupingUsed(false);
                StopWatch.TaskInfo[] var7;
                int var6 = (var7 = this.getTaskInfo()).length;
    
                for (int var5 = 0; var5 < var6; ++var5) {
                    StopWatch.TaskInfo task = var7[var5];
                    sb.append(nf.format(task.getTimeMillis())).append("  ");
                    sb.append(pf.format(task.getTimeSeconds() / this.getTotalTimeSeconds())).append("  ");
                    sb.append(task.getTaskName()).append("
    ");
                }
            }
    
            return sb.toString();
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder(this.shortSummary());
            if (this.keepTaskList) {
                StopWatch.TaskInfo[] var5;
                int var4 = (var5 = this.getTaskInfo()).length;
    
                for (int var3 = 0; var3 < var4; ++var3) {
                    StopWatch.TaskInfo task = var5[var3];
                    sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
                    long percent = Math.round(100.0D * task.getTimeSeconds() / this.getTotalTimeSeconds());
                    sb.append(" = ").append(percent).append("%");
                }
            } else {
                sb.append("; no task info kept");
            }
    
            return sb.toString();
        }
    
        public static final class TaskInfo {
            private final String taskName;
            private final long timeMillis;
    
            TaskInfo(String taskName, long timeMillis) {
                this.taskName = taskName;
                this.timeMillis = timeMillis;
            }
    
            public String getTaskName() {
                return this.taskName;
            }
    
            public long getTimeMillis() {
                return this.timeMillis;
            }
    
            public double getTimeSeconds() {
                return (double) this.timeMillis / 1000.0D;
            }
        }
    
    }
    

      感谢这几位大佬的文章:

    唐磊(Jason) StopWatch方法详解

    love_best stopWatch

    BarryWang 计时器StopWatch的几种写法

    Stopwatch 类
    命名空间:System.Diagnostics.Stopwatch

    实例化:Stopwatch getTime=new Stopwatch();

    开始计时:getTime.Start();

    getTime.Stop();

    Console.WriteLine("getTime:"+totleTime .ElapsedMilliseconds.ToString ());

    常用属性:Stopwatch.Elapsed 获取当前实例测量得出的总运行时间。

    Stopwatch.ElapsedMilliseconds 获取当前实例测量得出的总运行时间(以毫秒计时)。

    Stopwatch.ElapsedTicks 获取当前实例测量得出的总运行时间(用计时器计时周期表示)。

         Stopwatch.IsRunning 获取一个指示Stopwatch计时器是否在运行的值

    学spring的时候,看到关于统计时间的课就好奇,记录下来,以后可以直接用

    org.springframework.util .秒表

    秒表这个类在计时时必须关闭以前的对象来创建一个新的秒表。统计完成后,只需要像报表一样输出来显示统计的时间。

    在开发中,经常使用System.currentTimeMillis()来计算时间。做统计,执行完毕,

    你仍然需要减去才能得到最终的时间值,但是秒表几乎是一个类似的功能。

    狗屁不通文章生成器网页版(AI版,不再狗屁不通)

    远离外包公司,就是远离码农的血汗工厂

    人工智能是铁饭碗还是铁坑,看看人工智能博士怎么说

    python调用接口,python接收post请求接口(附完整代码)

    自动写文章的智能软件(基于AI写作)

  • 相关阅读:
    git_02_git常用操作命令
    git_01_上传第一个项目至git
    Jenkins持续集成_04_解决HTML测试报告样式丢失问题
    Jenkins持续集成_03_添加测试报告
    Jenkins持续集成_02_添加python项目&设置定时任务
    Jenkins持续集成_01_Mac安装配置
    Mac获取Jenkins管理员初始密码
    (appium+python)UI自动化_10_adb常用命令
    安卓monkey自动化测试,软硬回车
    冒烟测试
  • 原文地址:https://www.cnblogs.com/python168/p/13872187.html
Copyright © 2011-2022 走看看