标签:TimeWatchUtil watchName 程序 tw 耗时 TimeWatch outStr sections append
调用方法:
/**
*
* @title: 程序时间统计类TimeWatchUtil
* @time: 2019年5月28日
*
*/
public class TimeWatchTest {
public static void main(String[] args) throws InterruptedException {
TimeWatchUtil tw = TimeWatchUtil.createTimeWatch("TtimeWatch====:");
Thread.sleep(100);
tw.addStep("步骤1");
Thread.sleep(200);
tw.addStep("步骤2");
Thread.sleep(300);
tw.addStep("步骤3");
Thread.sleep(400);
tw.addStep("步骤4");
if (tw != null) {
System.out.println(tw.outputTimeList());
}
//[TIMEWATCH] TtimeWatch====::1000 [DETAILS] 步骤1:100 步骤2:200 步骤3:300 步骤4:400
}
}
工具类:
package com.tt;
import java.util.ArrayList;
import java.util.List;
/**
*
* description:主要用来调试程序,观察程序运行每一步所用的时间
*
*/
public class TimeWatchUtil {
/**
* 创建开始时间
*/
private long start;
/**
* 节点数据(一般保存结束时间数据)
*/
private List<Object> sections;
/**
* 节点名称
*/
private List<Object> sectionNames;
/**
* 观察名称,下面包括很多的sectionNames,watchName属于sectionNames的整体,便于观察定位
*/
private String watchName;
public static TimeWatchUtil createTimeWatch(String watchName) {
TimeWatchUtil timeWatch = new TimeWatchUtil();
timeWatch.watchName = watchName;
return timeWatch;
}
protected TimeWatchUtil() {
sections = new ArrayList<>();// 初始化sections
sectionNames = new ArrayList<>();// 初始化sectionNames
start = System.currentTimeMillis();// 当前系统开始时间
}
public void addStep(String stepName) {
sectionNames.add(stepName);// 每一步节点的名称
sections.add(Long.valueOf(System.currentTimeMillis()));// 节点开始时间
}
public String outputTimeList() {
watchName = watchName.trim();
StringBuffer outStr = new StringBuffer();// 整体观察名称
outStr.append("[TIMEWATCH] ");
outStr.append(watchName);
outStr.append(":");
outStr.append(" [DETAILS] ");
// 输出每一个节点的名称和花费时间
long last = start;
for (int i = 0; i < sections.size(); i++) {
long temp = ((Long) sections.get(i)).longValue();
outStr.append("" + (String) sectionNames.get(i) + ":");
outStr.append((double) (temp - last));
outStr.append(" ");
last = temp;
}
// 总体花费时间
long totalWaste = 0;
if (sections != null && sections.size() > 0) {
totalWaste = ((Long) sections.get(sections.size() - 1)).longValue() - start;
}
// 将总体花费时间插入到名称watchName的后面。"[TIMEWATCH] "和":"长度是13
outStr.insert(watchName.length() + 13, totalWaste);
return outStr.toString();
}
}
标签:TimeWatchUtil,watchName,程序,tw,耗时,TimeWatch,outStr,sections,append 来源: https://blog.csdn.net/qq_36521848/article/details/106204350
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。