Skip to content

简单停表,记录代码执行,debug时长的小工具

License

Notifications You must be signed in to change notification settings

Verlif/stopwatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stopwatch

简单的停表

异常简单的停表,主要是用于测试方法耗时什么的,就像现实的停表一样使用它。

功能

  • 记录间隔时间
  • 停表暂停
  • 关键时间点标记
  • 计时单位转换

使用

Stopwatch watch = Stopwatch.get("testWatch");
System.out.println("停表开始: " + watch.start());
Thread.sleep(123);
System.out.println("停表记录: " + watch.pin());
System.out.println("间隔时间: " + watch.getLastInterval(TimeUnit.MILLISECONDS));
Thread.sleep((long) (Math.random() * 1000));
System.out.println("停表记录: " + watch.pin("开始吃饭"));
Thread.sleep((long) (Math.random() * 1000));
System.out.println("停表记录: " + watch.pin("吃完"));
System.out.println("吃饭时间: " + watch.getPinInterval("开始吃饭", "吃完", TimeUnit.MILLISECONDS));
System.out.println("停表结束: " + watch.stop());
System.out.println("打点时间:" + Arrays.toString(watch.getIntervalLine(TimeUnit.MILLISECONDS).toArray()));
watch.restart();

输出如下:

停表开始: 1494107535848000
停表记录: 1494107661364700
间隔时间: 125.5167
停表记录: 1494108176334400
停表记录: 1494108683787000
吃饭时间: 507.4526
停表结束: 1494108683924800
打点时间:[125.5167, 514.9697, 507.4526, 0.1378]

注意: 停表只有在reset之后才可以再次start,或是直接使用restart

常用方法

Stopwatch.start(name)

通过Stopwatch来启动停表,用于多个停表的处理。可通过Stopwatch.get(name)来获取对应停表。

当然,也可以直接new Stopwatch()

pin

主要的计时打点方法,使用watch.pinwatch.pin(name)来记录时间点。

getPinInterval

获取打点pin的间隔时间,例如:

  • watch.getPinInterval(2, TimeUnit.MILLISECONDS)) - 获取第二个pin点与上一个pin点之间的间隔时间
  • watch.getPinInterval("吃饭", TimeUnit.MILLISECONDS)) - 获取名为吃饭的pin点与上一个pin点之间的间隔时间
  • watch.getPinInterval("开始吃饭", "吃完", TimeUnit.MILLISECONDS)) - 获取名为开始吃饭的pin点与名为吃完的pin点之间的间隔时间

getIntervalLine

获取所有pin点的间隔时间列表

添加依赖

LastVersion : Release

  1. 添加Jitpack仓库源

    maven

    <repositories>
       <repository>
           <id>jitpack.io</id>
           <url>https://jitpack.io</url>
       </repository>
    </repositories>

    Gradle

    allprojects {
      repositories {
          maven { url 'https://jitpack.io' }
      }
    }
    
  2. 添加依赖

    maven

       <dependencies>
           <dependency>
               <groupId>com.github.Verlif</groupId>
               <artifactId>stopwatch</artifactId>
               <version>版本号</version>
           </dependency>
       </dependencies>

    Gradle

    dependencies {
      implementation 'com.github.Verlif:stopwatch:版本号'
    }
    

About

简单停表,记录代码执行,debug时长的小工具

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages