• 周五. 4月 26th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

计算程序耗时工具

admin

11月 28, 2021

计算程序耗时工具

public class TimeConsumptionDemo {

	public static void main(String[] args) throws InterruptedException {
		long begin = System.currentTimeMillis(); 
		Thread.sleep(5000);
		long end = System.currentTimeMillis();
		System.out.println(String.format("程序执行耗时:%s", diffTime(begin,end)));
	}
	
	
	/**
	 * Desc: 计算时间差  方式一
	 * long begin = System.currentTimeMillis(); 
	 * long end = System.currentTimeMillis();
	 * @param begin
	 * @param end
	 * @return
	 */
	public static String diffTime(long begin, long end) {
		long diff = Math.abs(end - begin);
		long days = diff / (1000 * 60 * 60 * 24);   
		long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60); //获取时 
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);  //获取分钟
        long s=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
        return hours+"时"+minutes+"分"+s+"秒";
	}
	
	

}

方式2:使用org.apache.commons.lang3.time.StopWatch 计时

import org.apache.commons.lang3.time.StopWatch;
StopWatch sw = StopWatch.createStarted();
Thread.sleep(5000);
System.out.println(String.format("程序执行耗时:%s", sw));
边系鞋带边思考人生.

《计算程序耗时工具》有一个想法
  1. Wow, amazing weblog structure! How lengthy have you been running a blog
    for? you made running a blog look easy. The entire look of
    your website is great, as smartly as the content! You can see similar here sklep online

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注