一、總結(jié)
1.1、使用System.currentTimeMillis();計(jì)算程序執(zhí)行毫秒數(shù)
// 開始時(shí)間1
long startTime1 = System.currentTimeMillis();
Thread.sleep(100);
// 結(jié)束時(shí)間1
long endTime1 = System.currentTimeMillis();
// 開始時(shí)間2
long startTime2 = System.currentTimeMillis();
Thread.sleep(200);
// 結(jié)束時(shí)間2
long endTime2 = System.currentTimeMillis();
System.out.println("邏輯1執(zhí)行時(shí)間:"+ (endTime1 - startTime1));
System.out.println("邏輯2執(zhí)行時(shí)間:"+ (endTime2 -startTime2));
1.2、使用org.springframework.util包下的一個(gè)工具類StopWatch計(jì)算執(zhí)行時(shí)間
StopWatch testTask = new StopWatch("TestTask");
// 記錄開始時(shí)間點(diǎn)
testTask.start("task1");
Thread.sleep(100);
// 記錄結(jié)束時(shí)間點(diǎn)
testTask.stop();
// 記錄開始時(shí)間點(diǎn)
testTask.start("task2");
Thread.sleep(200);
// 記錄結(jié)束時(shí)間點(diǎn)
testTask.stop();
// 輸出執(zhí)行時(shí)間
System.out.println("==任務(wù)執(zhí)行時(shí)間==");
System.out.println(testTask.prettyPrint());
System.out.println("執(zhí)行任務(wù)的毫秒數(shù):"+testTask.getTotalTimeMillis());
1.3兩個(gè)案例的完整代碼、執(zhí)行結(jié)果
package time.stopwatch;
import org.springframework.util.StopWatch;
public class StopWatchTest {
public static void main(String[] args) throws InterruptedException {
// 計(jì)算執(zhí)行時(shí)間
calculateExecuteTime1();
// 計(jì)算執(zhí)行時(shí)間
calculateExecuteTime2();
}
public static void calculateExecuteTime1() throws InterruptedException {
// 開始時(shí)間1
long startTime1 = System.currentTimeMillis();
Thread.sleep(100);
// 結(jié)束時(shí)間1
long endTime1 = System.currentTimeMillis();
// 開始時(shí)間2
long startTime2 = System.currentTimeMillis();
Thread.sleep(200);
// 結(jié)束時(shí)間2
long endTime2 = System.currentTimeMillis();
System.out.println("邏輯1執(zhí)行時(shí)間:"+ (endTime1 - startTime1));
System.out.println("邏輯2執(zhí)行時(shí)間:"+ (endTime2 -startTime2));
}
public static void calculateExecuteTime2() throws InterruptedException {
StopWatch testTask = new StopWatch("TestTask");
// 記錄開始時(shí)間點(diǎn)
testTask.start("task1");
Thread.sleep(100);
// 記錄結(jié)束時(shí)間點(diǎn)
testTask.stop();
// 記錄開始時(shí)間點(diǎn)
testTask.start("task2");
Thread.sleep(200);
// 記錄結(jié)束時(shí)間點(diǎn)
testTask.stop();
// 輸出執(zhí)行時(shí)間
System.out.println("==任務(wù)執(zhí)行時(shí)間==");
System.out.println(testTask.prettyPrint());
System.out.println("執(zhí)行任務(wù)的毫秒數(shù):"+testTask.getTotalTimeMillis());
}
}
執(zhí)行結(jié)果:
邏輯1執(zhí)行時(shí)間:109
邏輯2執(zhí)行時(shí)間:203
==任務(wù)執(zhí)行時(shí)間==
StopWatch 'TestTask': running time = 319076700 ns
---------------------------------------------
ns % Task name
---------------------------------------------
114748000 036% task1
204328700 064% task2
執(zhí)行任務(wù)的毫秒數(shù):319
1.4?StopWatch優(yōu)缺點(diǎn):
優(yōu)點(diǎn):
1、spring自帶工具類,可直接使用
2、代碼實(shí)現(xiàn)簡單,使用更簡單
3、統(tǒng)一歸納,展示每項(xiàng)任務(wù)耗時(shí)與占用總時(shí)間的百分比,展示結(jié)果直觀
4、性能消耗相對(duì)較小,并且最大程度的保證了start與stop之間的時(shí)間記錄的準(zhǔn)確性
5、可在start時(shí)直接指定任務(wù)名字,從而更加直觀的顯示記錄結(jié)果
缺點(diǎn):
1、一個(gè)StopWatch實(shí)例一次只能開啟一個(gè)task,不能同時(shí)start多個(gè)task,并且在該task未stop之前不能start一個(gè)新的task,必須在該task stop之后才能開啟新的task,若要一次開啟多個(gè),需要new不同的StopWatch實(shí)例
2、代碼侵入式使用,需要改動(dòng)多處代碼文章來源:http://www.zghlxwxcb.cn/news/detail-830579.html
1.5、spring中StopWatch源碼實(shí)現(xiàn)如下:
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
public class StopWatch {
private final String id;
private boolean keepTaskList = true;
private final List<TaskInfo> taskList = new LinkedList();
private long startTimeMillis;
private boolean running;
private String currentTaskName;
private StopWatch.TaskInfo lastTaskInfo;
private int taskCount;
private long totalTimeMillis;
public StopWatch() {
this.id = "";
}
public StopWatch(String id) {
this.id = id;
}
public void setKeepTaskList(boolean keepTaskList) {
this.keepTaskList = keepTaskList;
}
public void start() throws IllegalStateException {
this.start("");
}
public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
} else {
this.startTimeMillis = System.currentTimeMillis();
this.running = true;
this.currentTaskName = taskName;
}
}
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
} else {
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new StopWatch.TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.running = false;
this.currentTaskName = null;
}
}
public boolean isRunning() {
return this.running;
}
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task interval");
} else {
return this.lastTaskInfo.getTimeMillis();
}
}
public String getLastTaskName() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task name");
} else {
return this.lastTaskInfo.getTaskName();
}
}
public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task info");
} else {
return this.lastTaskInfo;
}
}
public long getTotalTimeMillis() {
return this.totalTimeMillis;
}
public double getTotalTimeSeconds() {
return (double) this.totalTimeMillis / 1000.0D;
}
public int getTaskCount() {
return this.taskCount;
}
public StopWatch.TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
} else {
return (StopWatch.TaskInfo[]) this.taskList.toArray(new StopWatch.TaskInfo[this.taskList.size()]);
}
}
public String shortSummary() {
return "StopWatch '" + this.id + "': running time (millis) = " + this.getTotalTimeMillis();
}
public String prettyPrint() {
StringBuilder sb = new StringBuilder(this.shortSummary());
sb.append('\n');
if (!this.keepTaskList) {
sb.append("No task info kept");
} else {
sb.append("-----------------------------------------\n");
sb.append("ms % Task name\n");
sb.append("-----------------------------------------\n");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
StopWatch.TaskInfo[] var7;
int var6 = (var7 = this.getTaskInfo()).length;
for (int var5 = 0; var5 < var6; ++var5) {
StopWatch.TaskInfo task = var7[var5];
sb.append(nf.format(task.getTimeMillis())).append(" ");
sb.append(pf.format(task.getTimeSeconds() / this.getTotalTimeSeconds())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(this.shortSummary());
if (this.keepTaskList) {
StopWatch.TaskInfo[] var5;
int var4 = (var5 = this.getTaskInfo()).length;
for (int var3 = 0; var3 < var4; ++var3) {
StopWatch.TaskInfo task = var5[var3];
sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
long percent = Math.round(100.0D * task.getTimeSeconds() / this.getTotalTimeSeconds());
sb.append(" = ").append(percent).append("%");
}
} else {
sb.append("; no task info kept");
}
return sb.toString();
}
public static final class TaskInfo {
private final String taskName;
private final long timeMillis;
TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName;
this.timeMillis = timeMillis;
}
public String getTaskName() {
return this.taskName;
}
public long getTimeMillis() {
return this.timeMillis;
}
public double getTimeSeconds() {
return (double) this.timeMillis / 1000.0D;
}
}
}
原文摘錄至:https://blog.csdn.net/gxs1688/article/details/87185030
致敬原作者:一個(gè)不二,侵刪。文章來源地址http://www.zghlxwxcb.cn/news/detail-830579.html
到了這里,關(guān)于Java項(xiàng)目計(jì)算程序執(zhí)行時(shí)間方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!