TimeInterval
Introduction
Hutool implements a timer function by encapsulating TimeInterval
, which allows for calculating the execution time of methods or processes.
TimeInterval
supports grouped timing for easy comparison of time.
Usage
TimeInterval timer = DateUtil.timer();
//---------------------------------
//-------This is the process to be executed.
//---------------------------------
timer.interval(); // Time spent in milliseconds
timer.intervalRestart(); // Restart the timer and return elapsed time
timer.intervalMinute(); // Time spent in minutes
Grouped timing can also be implemented:
final TimeInterval timer = new TimeInterval();
// Group 1
timer.start("1");
ThreadUtil.sleep(800);
// Group 2
timer.start("2");
ThreadUtil.sleep(900);
Console.log("Timer 1 took {} ms", timer.intervalMs("1"));
Console.log("Timer 2 took {} ms", timer.intervalMs("2"));