统计代码耗时的工具

举报
琴岛蛏子 发表于 2022/03/21 00:07:40 2022/03/21
【摘要】 统计代码耗时的工具项目中通常会通过打印时间来查看某段任务的耗时,进行项目优化通常会通过t2-t1的方式进行统计public static void main(String[] args) throws InterruptedException { StopWatchTest.test0();// StopWatchTest.test1();}public static...

统计代码耗时的工具

项目中通常会通过打印时间来查看某段任务的耗时,进行项目优化
通常会通过t2-t1的方式进行统计

public static void main(String[] args) throws InterruptedException {
     StopWatchTest.test0();
//        StopWatchTest.test1();
}

public static void test0() throws InterruptedException {
     long start = System.currentTimeMillis();
     // do something
     Thread.sleep(100);
    long end = System.currentTimeMillis();
    long start2 = System.currentTimeMillis();
    // do something
    Thread.sleep(200);
    long end2 = System.currentTimeMillis();
    System.out.println("任务1执行耗时:" + (end - start));
    System.out.println("任务2执行耗时:" + (end2 - start2));
}

spring StopWatch用法

spring-framework提供了一个StopWatch类可以做类似任务执行时间控制,也就是封装了一个对开始时间,结束时间记录操作的Java类,小例一则如下

栗子:

package com.example.stopwatch;

import org.springframework.util.StopWatch;


public class TestStopWatch {
    private void test() throws InterruptedException {
        StopWatch sw = new StopWatch();

        sw.start("任务1");
        Thread.sleep(1000);
        sw.stop();

        sw.start("任务2");
        Thread.sleep(2000);
        sw.stop();

        sw.start("任务3");
        Thread.sleep(500);
        sw.stop();

        System.out.println(sw.prettyPrint());
        System.out.println(sw.getTotalTimeMillis());
        System.out.println(sw.getLastTaskName());
        System.out.println(sw.getLastTaskInfo());
        System.out.println(sw.getTaskCount());
    }


    public static void main(String []argv) throws InterruptedException {
        TestStopWatch testStopWatch = new TestStopWatch();
        testStopWatch.test();
    }
}

结果

StopWatch '': running time (millis) = 3518
-----------------------------------------
ms     %     Task name
-----------------------------------------
00998  028%  任务1
02020  057%  任务2
00500  014%  任务3
 
3518
任务3
org.springframework.util.StopWatch$TaskInfo@5b2133b1
3

一个StopWatch实例一次只能开启一个task,不能同时start多个task,并且在该task未stop之前不能start一个新的task,必须在该task stop之后才能开启新的task,若要一次开启多个,需要new不同的StopWatch实例.

计时器工具-TimeInterval

Hutool通过封装TimeInterval实现计时器功能,即可以计算方法或过程执行的时间。

TimeInterval支持分组计时,方便对比时间。

使用

TimeInterval timer = DateUtil.timer();

//---------------------------------
//-------这是执行过程
//---------------------------------

timer.interval();//花费毫秒数
timer.intervalRestart();//返回花费时间,并重置开始时间
timer.intervalMinute();//花费分钟数

也可以实现分组计时:

final TimeInterval timer = new TimeInterval();

// 分组1
timer.start("1");
ThreadUtil.sleep(800);

// 分组2
timer.start("2");
ThreadUtil.sleep(900);

Console.log("Timer 1 took {} ms", timer.intervalMs("1"));
Console.log("Timer 2 took {} ms", timer.intervalMs("2"));
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。