zoukankan      html  css  js  c++  java
  • Guava Stopwatch

    Stopwatch 解释为计时器,又称秒表、停表,是记录时间的。

    使用System.currentTimeMillis()也能统计这段代码的执行时间,那么为什么还会有Stopwatch

    官方称不直接使用System#nanoTime是有一下几个原因:

    • 时间源可以替代 可以重写Ticker

    • nanoTime的返回值是纳秒,返回的值没有意义,Stopwatch抽象返回值

    Stopwatch stopwatch = Stopwatch.createStarted();
    
    long startTime = System.currentTimeMillis();
    for (long i = 0; i < 1000000000L; i++) {
        // do some thing
    }
    System.out.println("逻辑代码运行耗时:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
    System.out.println(System.currentTimeMillis() - startTime);
    
    // stopwatch.stop();//暂停
    stopwatch.reset();//重置
    stopwatch.start();//启动
    for (long i = 0; i < 1000000000L; i++) {
        // do some thing
    }
    System.out.println("逻辑代码运行耗时:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
    System.out.println(System.currentTimeMillis() - startTime);
    
    for (long i = 0; i < 1000000000L; i++) {
        // do some thing
    }
    System.out.println("逻辑代码运行耗时:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
    System.out.println(System.currentTimeMillis() - startTime);
  • 相关阅读:
    法院
    Spring Cloud常用组件
    PowerShell使用教程
    浅谈3DES加密解密
    SC win consul
    SB-Token-Jwt
    前端MVC Vue2学习总结
    spring-session-data-redis
    SpringBoot WS
    SpringBoot之使用Spring Session集群-redis
  • 原文地址:https://www.cnblogs.com/ooo0/p/15011073.html
Copyright © 2011-2022 走看看