Java计时新姿势

  • 2019 年 10 月 3 日
  • 筆記

????????????????????
??? ?????????????????????????????
?????StopWatch????????????!

????????


??????????????????????????????????????????????????????????? Java ????????????????????

public static void main(String[] args) {      Long startTime = System.currentTimeMillis();        doSomeThing();        Long endTime = System.currentTimeMillis();      Long elapsedTime = (endTime - startTime) / 1000;      System.out.println("?????" + elapsedTime + "s");  }  // ????????  private static void doSomeThing()  {      try {          Thread.sleep(1000);      } catch (InterruptedException e) {          e.printStackTrace();      }  }

??????????????????????????????????????????????????????????????????????

??StopWatch ?


?????????????? Maven ??? Spring ?????? Spring MVC ? Spring Boot ???????????

<!-- spring??? -->  <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-core</artifactId>      <version>${spring.version}</version>  </dependency>

????????????????????

public static void main(String[] args) {      StopWatch clock = new StopWatch();        clock.start("?????");      doSomeThing();      clock.stop();        clock.start("?????");      doSomeThing();      clock.stop();        System.out.println(clock.prettyPrint());  }    // ????????  private static void doSomeThing() {      try {          Thread.sleep(1000);      } catch (InterruptedException e) {          e.printStackTrace();      }  }

??????? StopWatch ???? prettyPrint() ?????????????????????????????????

StopWatch '': running time (millis) = 2009  -----------------------------------------  ms     %     Task name  -----------------------------------------  01005  050%  ?????  01004  050%  ?????

??????????????????????????????????????????????????

StopWatch ?????????

???? StopWatch ??????????? 200 ?????????????

    public void start(String taskName) throws IllegalStateException {          if (this.currentTaskName != null) {              throw new IllegalStateException("Can't start StopWatch: it's already running");          } else {              this.currentTaskName = taskName;              this.startTimeMillis = System.currentTimeMillis();          }      }        public void stop() throws IllegalStateException {          if (this.currentTaskName == null) {              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.currentTaskName = null;          }      }

???????? LinkedList ??????? taskList ????????????????? System.currentTimeMillis() ????????????????????????????? TaskInfo ???????? taskList ????

??? prettyPrint() ???????? taskList ?????????????????????

    public String shortSummary() {          return "StopWatch '" + this.getId() + "': 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 namen");              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[] var4 = this.getTaskInfo();              int var5 = var4.length;                for(int var6 = 0; var6 < var5; ++var6) {                  StopWatch.TaskInfo task = var4[var6];                  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();      }

????? get ??


??????????

?????????????
???????wmyskxz.com
??ID?@???????
github?wmyskxz
??????????wmyskxz
??????? & ???? & ??
???????????qq??3382693