java计算时间差(耗时计算)的三种方式

目录

一、System.currentTimeMillis()

二、StopWatch

    1、spring 用法

        ①、简单用法

        ②、说明

        ③、方法

        ④、详细用法

    2、apache 用法

        ①、简单用法

        ②、说明

        ③、方法

        ④、详细用法

一、System.currentTimeMillis()

	long startTime = System.currentTimeMillis();
	//业务代码开始
    Thread.sleep(100);
    //业务代码结束
    System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");

二、StopWatch

1、spring 用法

maven坐标

	<dependency>
         <groupId>springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>1.2.6</version>
    </dependency>

简单用法:

	StopWatch stopWatch = new StopWatch();
    stopWatch.start("任务一");
    Thread.sleep(100);
    stopWatch.stop();
    System.out.println("耗时:" + stopWatch.getTotalTimeMillis() + "ms");
    System.out.println("耗时:" + stopWatch.getTotalTimeSeconds() + "s");

说明:

	StopWatch stopWatch = new StopWatch();
	//计时开始
    stopWatch.start("任务一");
    //业务代码开始
    Thread.sleep(100);
    //业务代码结束
	//计时结束
    stopWatch.stop();
    System.out.println("耗时:" + stopWatch.getTotalTimeMillis() + "ms");
    System.out.println("耗时:" + stopWatch.getTotalTimeSeconds() + "s");

方法:

  • void start(“任务名称”):开始一个任务名称的计时
  • void stop():停止当前任务的计时
  • boolean isRunning():是否正在计时某任务
  • long getTotalTimeMillis():所有任务的总体执行时间(毫秒单位)
  • double getTotalTimeSeconds():所有任务的总时间(以秒为单位)
  • long getLastTaskTimeMillis():上一个任务的耗时(毫秒单位)
  • int getTaskCount():定时任务的数量
  • String prettyPrint():优美地打印所有任务的详细耗时情况
  • StopWatch.TaskInfo[] getTaskInfo():包含任务名称和任务耗时的实体类数组

详细用法:

package com.cfay.demo.stopWatch;

import org.springframework.util.StopWatch;

/**
 * @author lcb
 * @since 2022/7/3
 */
public class SpringStopWatchDemo {

    public static void main(String[] args) throws Exception {
        testStopWatch();
    }

    private static void testStopWatch() throws Exception {
        //创建一个指定了id的StopWatch
        StopWatch stopWatch = new StopWatch("任务管理");
        //是否正在计时某任务
        if (stopWatch.isRunning()) {
            System.out.println("正在计时某任务");
        } else {
            System.out.println("没有计时任务");
        }
        //开始任务一的计时
        stopWatch.start("任务一");
        //业务1 代码开始
        Thread.sleep(100);
        //业务1 代码结束
        //停止当前任务的计时
        stopWatch.stop();
        //业务2 代码开始
        stopWatch.start("任务二");
        Thread.sleep(200);
        //业务2 代码结束
        stopWatch.stop();

        //包含任务名称和任务耗时的实体类数组
        StopWatch.TaskInfo[] taskInfo = stopWatch.getTaskInfo();
        for (StopWatch.TaskInfo info : taskInfo) {
            System.out.println(info.getTaskName() + ":耗时" + info.getTimeMillis() + "ms");
            System.out.println(info.getTaskName() + ":耗时" + info.getTimeSeconds() + "s");
        }
        System.out.println("总任务数:" + stopWatch.getTaskCount() + "个");
        System.out.println("上一个任务耗时:" + stopWatch.getLastTaskTimeMillis() + "ms");
        System.out.println("总耗时:" + stopWatch.getTotalTimeMillis() + "ms");
        //优美地打印所有任务的详细耗时情况
        System.out.println(stopWatch.prettyPrint());

    }
}

2、apache 用法

maven坐标

	<dependency>
	      <groupId>springframework</groupId>
	      <artifactId>spring-core</artifactId>
	      <version>1.2.6</version>
	</dependency>

简单用法:

	StopWatch stopWatch = StopWatch.createStarted();
	Thread.sleep(100);
	stopWatch.stop();
	System.out.println("耗时:" + stopWatch.getTime(TimeUnit.MILLISECONDS) + "ms");

说明:

	//计时开始
	StopWatch stopWatch = StopWatch.createStarted();
	//业务代码开始
	Thread.sleep(100);
	//业务代码结束
	//计时结束
	stopWatch.stop();
	System.out.println("耗时:" + stopWatch.getTime(TimeUnit.MILLISECONDS) + "ms");

方法:

  • void start():开始计时
  • void stop():停止计时
  • void suspend():暂停秒表
  • void resume():恢复秒表
  • void reset():重置秒表
  • void split():设置split点
  • void unsplit():取消split点
  • long getSplitTime():获取从开始计时点到最新设置的split点之间的时间间隔
  • long getTime():获取从开始计时到现在的时间间隔,可以通过TimeUnit来指定返回的时间单位
  • boolean isStarted():是否开始计时
  • boolean isSuspended() :是否暂停计时
  • boolean isStopped():是否结束计时

详细用法:

package com.cfay.demo.stopWatch;


import org.apache.commons.lang3.time.StopWatch;

import java.util.concurrent.TimeUnit;

/**
 * @author lcb
 * @since 2022/7/3
 */
public class Lang3StopWatchDemo {

    public static void main(String[] args) throws Exception {
        testStopWatch();
    }

    private static void testStopWatch() throws Exception {
        //method one
        //创建秒表
        StopWatch stopWatch = new StopWatch();
        //计时开始
        stopWatch.start();
        Thread.sleep(100);
        stopWatch.stop();
        System.out.println("耗时:" + stopWatch.getTime(TimeUnit.MILLISECONDS) + "ms" + "\n" +
                "耗时:" + stopWatch.getTime(TimeUnit.MINUTES) + "min" + "\n" +
                "------------------method split line------------------");

        //method two
        //创建秒表并开始计时
        StopWatch stopWatch2 = StopWatch.createStarted();
        //业务1 代码开始
        Thread.sleep(100);
        //业务1 代码结束
        long toBusinessOneCost = stopWatch2.getTime();
        System.out.println("业务1耗时: " + toBusinessOneCost + "ms");
        //业务2 代码开始
        Thread.sleep(200);
        //业务2 代码结束
        long toBusinessTwoCost = stopWatch2.getTime();
        System.out.println("业务2耗时: " + (toBusinessTwoCost - toBusinessOneCost) + "ms");
        //设置split点
        stopWatch2.split();
        System.out.println("split点耗时: " + stopWatch2.getSplitTime() + "ms");
        //取消split点
        stopWatch2.unsplit();
        //暂停秒表
        stopWatch2.suspend();
        //是否暂停计时
        if (stopWatch2.isSuspended()){
            System.out.println("暂停计时");
        }
        //业务3 代码开始
        Thread.sleep(300);
        //业务3 代码结束
        //恢复秒表
        stopWatch2.resume();
        if (stopWatch2.isStarted()) {
            stopWatch2.stop();
        }
        System.out.println("除了业务3,其他业务的总耗时:" + stopWatch2.getTime() + "ms");
        //重置秒表
        stopWatch2.reset();
        if (stopWatch2.isStopped()) {
            stopWatch2.start();
        }
        //业务4 代码开始
        Thread.sleep(400);
        //业务4 代码结束
        System.out.println("业务4耗时:" + stopWatch2.getTime() + "ms");
    }
}

在这里插入图片描述

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚风岸影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值