统计代码耗时的工具 StopWatch

简介: 项目中通常会通过打印时间来查看某段任务的耗时,进行项目优化

统计代码耗时的工具

项目中通常会通过打印时间来查看某段任务的耗时,进行项目优化
通常会通过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"));
相关文章
|
前端开发
若依框架---如何防止请求重复提交?
若依框架---如何防止请求重复提交?
2039 2
|
数据采集 缓存 安全
隐语小课|非平衡隐私集合求交(Unbalanced PSI)协议介绍
隐语小课|非平衡隐私集合求交(Unbalanced PSI)协议介绍
2026 0
|
Linux Docker 容器
CentOS7使用阿里源安装最新版Docker
CentOS7使用阿里源安装最新版Docker
11433 0
Java 中 `toList()` 与 `collect(Collectors.toList())` 的微妙差异:别再乱用了!
Java 中 `toList()` 与 `collect(Collectors.toList())` 的微妙差异:别再乱用了!
1067 0
|
Java 测试技术 API
我们来说说如何使用 Lambda 表达式实现排序功能
我是小假 期待与你的下一次相遇 ~
530 7
|
Python
将NC栅格表示时间维度的数据提取出来的方法
【10月更文挑战第20天】本文介绍了如何使用 Python 和 R 语言以及 ArcGIS 软件提取 netCDF 文件中的时间维度数据。首先,通过安装和导入必要的库(如 Python 的 `netCDF4` 和 `numpy`,R 的 `ncdf4`),打开 netCDF 文件并读取时间变量。接着,详细展示了 Python 和 R 的示例代码,说明了如何读取和处理时间数据。最后,介绍了在 ArcGIS 中添加 netCDF 文件、启用时间属性并提取时间维度数据的方法。
988 1
|
前端开发 JavaScript Java
java实现异步回调返回给前端
综上,Java中实现异步回调并将结果返回给前端是一项涉及后端异步处理和前端交互的综合任务。在实际项目中,开发人员需要根据应用需求和性能预期选择合适的异步模型与工具,并进行适当的配置和优化。
1175 3
|
缓存 安全 应用服务中间件
nginx配置proxy_set_header
nginx配置proxy_set_header
|
关系型数据库 数据库
关系型数据库设计规范第三范式(3NF)
【5月更文挑战第14天】关系型数据库设计规范第三范式(3NF)
1713 3

热门文章

最新文章