使用google 的Stopwatch
Stopwatch stopwatch = Stopwatch.createStarted();
doSomething();
stopwatch.stop(); // optional
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"
其实spring本身也是提供的.
StopWatch stopWatch = new StopWatch("任务耗时秒表工具");
stopWatch.start("task1");
Thread.sleep(1000);
stopWatch.stop();
System.out.println(stopWatch.getTotalTimeMillis());
stopWatch.start("task2");
Thread.sleep(3000);
stopWatch.stop();
//所有任务耗时时间
System.out.println(stopWatch.getTotalTimeMillis());
System.out.println(stopWatch.prettyPrint());
StopWatch stopWatch2 = new StopWatch("任务耗时秒表工具2");
stopWatch2.start("task3");
Thread.sleep(3000);
stopWatch2.stop();
//所有任务耗时时间
System.out.println(stopWatch2.getTotalTimeMillis());
System.out.println(stopWatch2.prettyPrint());