Java调试技巧 打印代码执行时间方法

  在调试代码时,我们想知道代码段的执行时间,废话不多说,上代码,毫秒级检测

// 放在要检测的代码段前,取开始前的时间戳
Long startTime = System.currentTimeMillis();

// 放在要检测的代码段后,取结束后的时间戳
Long endTime = System.currentTimeMillis();

// 计算并打印耗时
Long tempTime = (endTime - startTime);
System.out.println("花费时间:"+
	(((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
	((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
	((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
	((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
	((tempTime%1000)+"ms"));

执行结果(举例):
在这里插入图片描述


更新一下,这次直接提供一个工具类
分别提供了三个静态方法和三个实例方法
在只需要一次取值的时候,直接调用静态方法即可,
在需要多次分段打印的时候,需要创建对象然后调用实例方法。
多次调用实例打印方法时,同时输出每次打印之间的时间间隔。
打印方法提供了有参和无参两种,更灵活的使用。
(初始方法和打印方法自身执行时间基本在1ms以内)

/**
 * @author 散场前的温柔
 * @Description: 输出代码执行时间的工具类
 * @date 2020/11/20 13:45
 */
public class CodeRunningTimeUtils {
    private static Long startTimes;
    private static Long endTimes;
    private Long startTime;
    private Long endTime;
    private Long tempTimes;

    /**
     * 获取初始时间戳,静态方法
     */
    public static void initRunTimes(){
        startTimes = System.currentTimeMillis();
    }
    
	/**
     * 获取结束时间戳并打印代码执行时间,静态方法,无参
     */
    public static void printRunTimes(){
        printRunTimes(null);
    }

    /**
     * 获取结束时间戳并打印代码执行时间,静态方法,有参
     * @param str 字符串会在打印前输出
     */
    public static void printRunTimes(String str){
        if(str != null)
            str += " => ";
        else
            str = "";
        endTimes = System.currentTimeMillis();
        String classname = new Exception().getStackTrace()[1].getClassName(); //获取调用者的类名
        String method_name = new Exception().getStackTrace()[1].getMethodName(); //获取调用者的方法名
        if(startTimes != null) {
        Long tempTime = (endTimes - startTimes);
        System.out.println(str + classname + " > " + method_name + ":花费时间"+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
        }else{
            System.out.println("你忘记了放置静态初始方法了");
        }
    }

    /**
     * 获取初始时间戳
     */
    public void initRunTime(){
        startTime = System.currentTimeMillis();
    }

	/**
     * 获取结束时间戳并打印代码执行时间,无参
     */
    public void printRunTime(){
        printRunTime(null);
    }

   /**
     * 获取结束时间戳并打印代码执行时间,有参
     * 同时打印上次输出与本次输出之间的间隔时间
     */
    public void printRunTime(String str){
        if(str != null)
            str += " => ";
        else
            str = "";
        endTime = System.currentTimeMillis();
        String classname = new Exception().getStackTrace()[1].getClassName(); //获取调用者的类名
        String method_name = new Exception().getStackTrace()[1].getMethodName(); //获取调用者的方法名
        if(startTime != null) {
            Long tempTime = (endTime - startTime);
            System.out.print(str + classname + " > " + method_name + ":花费时间" +
                    (((tempTime / 86400000) > 0) ? ((tempTime / 86400000) + "d") : "") +
                    ((((tempTime / 86400000) > 0) || ((tempTime % 86400000 / 3600000) > 0)) ? ((tempTime % 86400000 / 3600000) + "h") : ("")) +
                    ((((tempTime / 3600000) > 0) || ((tempTime % 3600000 / 60000) > 0)) ? ((tempTime % 3600000 / 60000) + "m") : ("")) +
                    ((((tempTime / 60000) > 0) || ((tempTime % 60000 / 1000) > 0)) ? ((tempTime % 60000 / 1000) + "s") : ("")) +
                    ((tempTime % 1000) + "ms"));
            if(tempTimes != null){
                tempTime = (endTime - tempTimes);
                System.out.print(" \t间隔时间" +
                        (((tempTime / 86400000) > 0) ? ((tempTime / 86400000) + "d") : "") +
                        ((((tempTime / 86400000) > 0) || ((tempTime % 86400000 / 3600000) > 0)) ? ((tempTime % 86400000 / 3600000) + "h") : ("")) +
                        ((((tempTime / 3600000) > 0) || ((tempTime % 3600000 / 60000) > 0)) ? ((tempTime % 3600000 / 60000) + "m") : ("")) +
                        ((((tempTime / 60000) > 0) || ((tempTime % 60000 / 1000) > 0)) ? ((tempTime % 60000 / 1000) + "s") : ("")) +
                        ((tempTime % 1000) + "ms"));
            }
            System.out.println();
            tempTimes = System.currentTimeMillis();
        }else{
            System.out.println("你忘记了放置初始方法了");
        }
    }
}

调用示例:

public class Test {
	public static void main (String[] agrs) {
		CodeRunningTimeUtils.initRunTimes();
		测试代码块a
		CodeRunningTimeUtils.printRunTimes();
		//或者
		//CodeRunningTimeUtils.printRunTimes("测试代码块a");
		
		CodeRunningTimeUtils crtu = new CodeRunningTimeUtils();
		crtu.initRunTime();
		// 测试代码1
		crtu.printRunTime();
		// 测试代码2
		crtu.printRunTime("测试代码2");
	}
}
  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值