当前位置:网站首页>计算程序运行时间 demo
计算程序运行时间 demo
2022-07-29 06:46:00 【你若不离不弃,我必生死相依】
currentTimeMillis与StopWatch 的时间差工具:
计算时间差(天,小时,分钟):博客
Java 实例 时间的处理demo:博客
StopWatch
主要方法: 方法start():开始计时
stop():停止计时
suspend():暂停秒表
resume():恢复秒表
reset():重置秒表
split():设定split点
getSplitTime():获取时间,时间为从计时起点到最新设置的split点之间的时长
getTime():显示当前秒表时间,可以通过指定TimeUnit来指定返回的时间单位
import cn.hutool.core.date.StopWatch;
maven
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.2</version>
</dependency>
package com.example.democrud.democurd.controller;
//import org.apache.commons.lang.time.StopWatch;
import cn.hutool.core.date.StopWatch;
public class demoTest {
public static void main(String[] args) throws InterruptedException {
currentTimeMillis();
// StopWatch();//多线程适用任务
StopWatch01();//多线程适用任务 多线程执行demo
}
private static void StopWatch01() throws InterruptedException {
// 创建一个 StopWatch 实例
StopWatch sw = new StopWatch("实例");
// 开始计时
sw.start("任务1");
Thread.sleep(1000);
// 停止计时
sw.stop();
System.out.printf("任务1耗时:%d%s.\n", sw.getLastTaskTimeMillis(), "ms");
sw.start("任务2");
Thread.sleep(1100);
sw.stop();
System.out.printf("任务2耗时:%d%s.\n", sw.getLastTaskTimeMillis(), "ms");
System.out.printf("任务数量:%s,总耗时:%ss.\n", sw.getTaskCount(), sw.getTotalTimeSeconds());
}
/* private static void StopWatch() { try{ StopWatch stop=new StopWatch(); //start stop.start();//开始计时 Thread.sleep(1000); stop.split();//设置split点 System.out.println("1==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间 Thread.sleep(500); stop.split();//设置split点 System.out.println("2==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间 Thread.sleep(1000); stop.split();//设置split点 System.out.println("3==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间 Thread.sleep(1000); stop.split();//设置split点 System.out.println("4==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间 System.out.println("5==>时间为"+stop.getTime()+"毫秒");//getTime()统计从start到现在的计时 //stop stop.stop();//停止计时 Thread.sleep(1000); System.out.println("6==>时间为"+stop.getTime()+"毫秒");//getTime()统计从start到现在的计时 } catch (InterruptedException e) { e.printStackTrace(); } }*/
private static void currentTimeMillis() {
long startTime = System.currentTimeMillis();
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("Main 运行时间 :" + (endTime-startTime)/1000+"秒");
}
}
执行结果是:
Main 运行时间 :10秒
任务1耗时:999ms.
任务2耗时:1099ms.
任务数量:2,总耗时:2.098904701s.
Process finished with exit code 0
还有一个是引用的包不同调用的方法;
import org.apache.commons.lang.time.StopWatch;
package com.example.democrud.democurd.controller;
import org.apache.commons.lang.time.StopWatch;
//import cn.hutool.core.date.StopWatch;
public class demoTest {
public static void main(String[] args) throws InterruptedException {
// currentTimeMillis();
StopWatch();//多线程适用任务
// StopWatch01();//多线程适用任务 多线程执行demo
}
private static void StopWatch() {
try{
StopWatch stop=new StopWatch();
//start
stop.start();//开始计时
Thread.sleep(1000);
stop.split();//设置split点
System.out.println("1==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间
Thread.sleep(500);
stop.split();//设置split点
System.out.println("2==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间
Thread.sleep(1000);
stop.split();//设置split点
System.out.println("3==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间
Thread.sleep(1000);
stop.split();//设置split点
System.out.println("4==>时间为"+stop.getSplitTime()+"毫秒");//getSplitTime()获取从start 到 最后一次split的时间
System.out.println("5==>时间为"+stop.getTime()+"毫秒");//getTime()统计从start到现在的计时
//stop
stop.stop();//停止计时
Thread.sleep(1000);
System.out.println("6==>时间为"+stop.getTime()+"毫秒");//getTime()统计从start到现在的计时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
执行结果是
1==>时间为1001毫秒
2==>时间为1502毫秒
3==>时间为2502毫秒
4==>时间为3503毫秒
5==>时间为3503毫秒
6==>时间为3503毫秒
边栏推荐
- gin 服务退出
- MySQL advanced (Advanced) SQL statement (I)
- Summary of OCR optical character recognition methods
- 作业7.28 文件IO与标准IO
- vue-router路由缓存
- VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限
- MVFuseNet:Improving End-to-End Object Detection and Motion Forecasting through Multi-View Fusion of
- Flink real-time warehouse DWD layer (order placing multiple tables to realize join operation) template code
- Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
- LeetCode 879. 盈利计划
猜你喜欢

Why does ETL often become ELT or even let?

Image noise and matrix inversion

Error 1045 (28000) access denied for user 'root' @ 'localhost' solution

WPF 界面布局必知基础

gcc/g++的使用

外包干了3年,跳槽后转自动化测试工资是原来的2倍,秘诀原来是......

Student achievement ranking system based on C language design

个人博客系统(附源码)

Vmware16 create virtual machine: win11 cannot be installed

Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
随机推荐
暑期总结(二)
Flink实时仓库-DWD层(下单-多张表实现join操作)模板代码
Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
resize2fs: 超级块中的幻数有错(Bad magic number in super-block )
[C language brush leetcode] 2332. The latest time to get on the bus (m)
接口测试实战项目03:执行测试用例
JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?
Redis Basics
gin 中间件
When NPM is installed, it is stuck. There are five solutions
约瑟夫环问题
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
Pod基本介绍
SpingBoot整合Quartz框架实现动态定时任务(支持实时增删改查任务)
实现改变一段文字的部分颜色效果
Vmware16 create virtual machine: cannot create a new virtual machine, do not have permission to perform this operation
Explanation of suffix automata (SAM) + Luogu p3804 [template] suffix automata (SAM)
[Charles' daily problems] when you open Charles, you can't use nails
Some tips of vim text editor