当前位置: 首页 > news >正文

合肥网站制作前3名的宁波网站优化公司推荐

合肥网站制作前3名的,宁波网站优化公司推荐,功能型类的网站,北京住房和建设部网站首页StopWatch的使用 【一】简介【1】不使用StopWatch的案例【2】使用StopWatch的案例 【二】源码分析【1】查看源码【2】StopWatch优缺点#xff1a; 【一】简介 stopWatch是org.springframework.util包下的一个工具类#xff0c;使用它可直观的输出代码执行耗时#xff0c;以… StopWatch的使用 【一】简介【1】不使用StopWatch的案例【2】使用StopWatch的案例 【二】源码分析【1】查看源码【2】StopWatch优缺点 【一】简介 stopWatch是org.springframework.util包下的一个工具类使用它可直观的输出代码执行耗时以及执行时间百分比 【1】不使用StopWatch的案例 在未使用这个工具类之前如果我们需要统计某段代码的耗时我们会这样写 public static void main(String[] args) throws InterruptedException {test0(); }public static void test0() throws InterruptedException {long start System.currentTimeMillis();// do somethingThread.sleep(100);long end System.currentTimeMillis();long start2 System.currentTimeMillis();// do somethingThread.sleep(200);long end2 System.currentTimeMillis();System.out.println(某某1执行耗时: (end - start));System.out.println(某某2执行耗时: (end2 - start2)); }执行结果如下 某某1执行耗时:100 某某2执行耗时:200【2】使用StopWatch的案例 public class StopWatchDemo {public static void main(String[] args) throws InterruptedException {test1();}public static void test1() throws InterruptedException {StopWatch sw new StopWatch(test);sw.start(task1);// do somethingThread.sleep(100);sw.stop();sw.start(task2);// do somethingThread.sleep(200);sw.stop();System.out.println(sw.prettyPrint()~~~~~~~~~~~~~~~~~);System.out.println(sw.prettyPrint());}}运行结果如下 sw.prettyPrint()~~~~~~~~~~~~~~~~~ StopWatch test: running time (millis) 310 ----------------------------------------- ms % Task name ----------------------------------------- 00110 035% task1 00200 065% task2除此之外还有以下两个方法shortSummary,getTotalTimeMillis查看程序执行时间。 System.out.println(sw.shortSummary()~~~~~~~~~~~~~~~~~); System.out.println(sw.shortSummary()); System.out.println(sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~); System.out.println(sw.getTotalTimeMillis()); 运行结果 sw.shortSummary()~~~~~~~~~~~~~~~~~ StopWatch test: running time (millis) 308 sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~ 308【二】源码分析 【1】查看源码 其实以上内容在该工具类中实现也极其简单通过start与stop方法分别记录开始时间与结束时间其中在记录结束时间时会维护一个链表类型的tasklist属性从而使该类可记录多个任务最后的输出也仅仅是对之前记录的信息做了一个统一的归纳输出从而使结果更加直观的展示出来。 我们来看下源码 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 ListTaskInfo 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(Cant start StopWatch: its already running);} else {this.startTimeMillis System.currentTimeMillis();this.running true;this.currentTaskName taskName;}}public void stop() throws IllegalStateException {if (!this.running) {throw new IllegalStateException(Cant stop StopWatch: its 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: cant get last task interval);} else {return this.lastTaskInfo.getTimeMillis();}}public String getLastTaskName() throws IllegalStateException {if (this.lastTaskInfo null) {throw new IllegalStateException(No tasks run: cant 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: cant 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(\n);if (!this.keepTaskList) {sb.append(No task info kept);} else {sb.append(-----------------------------------------\n);sb.append(ms % Task name\n);sb.append(-----------------------------------------\n);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(\n);}}return sb.toString();}Overridepublic 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;}}}【2】StopWatch优缺点 优点 1.spring自带工具类可直接使用 2.代码实现简单使用更简单 3.统一归纳展示每项任务耗时与占用总时间的百分比展示结果直观性能消耗相对较小并且最大程度的保证了start与stop之间的时间记录的准确性 4.可在start时直接指定任务名字从而更加直观的显示记录结果。 缺点 1.一个StopWatch实例一次只能开启一个task不能同时start多个task并且在该task未stop之前不能,start一个新的task必须在该task stop之后才能开启新的task若要一次开启多个需要new不同的StopWatch实例 3.代码侵入式使用需要改动多处代码。
http://www.pierceye.com/news/194440/

相关文章:

  • 淘宝客做的最好的网站盐山建网站
  • 西城企业网站建设深圳设计网站多少钱
  • 电子商务网站建设a卷网站建设厘金手指排名二一
  • 网站空间便宜网站的信息管理建设的必要性
  • 校级特色专业建设网站博达站群网站建设教程
  • 有没有做任务的网站吗网站首页开发
  • 公司名字变了网站备案济南网站建设公司哪个好点呢
  • 图书馆网站建设的规章制度企业免费招聘网站
  • 效果图网站大全系统优化的例子
  • 京东的网站建设介绍网站开发要源码多少钱
  • 东莞网站制作公司报价企业定制
  • 创同盟做网站生成拼贴的网站
  • 网站备案号查电话号码商场网站开发
  • 手机网站建站教育模板下载泰州公司注册
  • 如何做商业网站推广西安市城乡建设管理局网站的公示栏
  • 上海做兼职哪个网站腾讯企业邮箱域名是什么
  • 霸州网站制作棋牌网站建设源码
  • 茶叶网站制作模板网页设计在安阳工资多少
  • 网站建设项目验收方案自己做捕鱼网站能不能挣钱
  • 微信网页网站怎么做我为群众办实事实践活动
  • 建设银行发卡银行网站福州 网站设计
  • 网站备案号码舟山高端网站建设
  • 买奢侈品代工厂做的产品的网站名建立网站 英语怎么说
  • 网站访问者qq计算机等级培训机构
  • 可以让外国人做问卷调查的网站济南优化seo网站建设公司
  • odoo做网站创建企业需要什么条件
  • 山西省旅游网站建设分析wordpress 个人介绍
  • 山东高级网站建设赚钱
  • 做网站大概要多少钱新建网站的外链多久生效
  • 天河区建设网站品牌网站建设小8蝌蚪