Brief introduction
Simple stopwatch, timer allows for multiple tasks running time publicly total operating time and each named task.
Hidden System.nanoTime()
use, thereby improving the readability of application code and reduces the possibility of calculation errors.
Note that this object is not designed to be thread-safe, and does not use sync.
Such properties typically used for authentication verification concept and development process, rather than as part production applications.
Spring Framework 5.2 from the start, in nanoseconds tracking and reporting uptime.
use
StopWatch
: Constructor, StopWatch can specify the id, the default is an empty string ( "")getId
: Get the StopWatchid
, can be specified by the constructor, no setter method, the default is ""setKeepTaskList
: Set thekeepTaskList
property, the default is true, if set to false, callsgetTaskInfo()
will complain, callprettyPrint()
, the print content format:
StopWatch 'test': running time = 300520200 ns
No task info kept
start
: StopWatch task starts running, you can enter parameters specifiedtaskName
, the default is an empty string ( "")currentTaskName
: Returns the currently running tasktaskName
, if StopWatch is not currently running, the returnnull
isRunning
: Returns whether StopWatch is still running instart()
andstop()
between calls is true, false otherwisestop
: StopWatch stop performing tasksgetTaskInfo
: StopWatch returned as an arraytaskList
, if thekeepTaskList
property is false, an exception is throwngetTaskCount
: Returns the number of tasks StopWatchgetLastTaskInfo
: Return to the previous task information (TaskInfo
objects), if not before running over tasks that have not been calledstop()
, an exception is throwngetLastTaskName
: Returns the name of the task on a task, that is,taskName
getLastTaskTimeNanos
: Return to the task of running time inns
getLastTaskTimeMillis
: Return to the task of running time inms
getTotalTimeNanos
: Return StopWatch total run time inns
getTotalTimeMillis
: Return StopWatch total run time inms
getTotalTimeSeconds
: Return StopWatch total run time ins
prettyPrint
: Perform all tasks StopWatch return after formatting, the format is:
StopWatch 'test': running time = 300009200 ns
---------------------------------------------
ns % Task name
---------------------------------------------
100146000 033% task1
199863200 067% task2
shortSummary
: Quick description StopWatch return running timetoString
: Returns all tasks simple execution of StopWatch information
Examples
StopWatch sw = new StopWatch("test");
System.out.println("id = " + sw.getId()); // id = test
// sw.setKeepTaskList(false);
sw.start("task1");
{
// do something
System.out.println("currentTaskName = " + sw.currentTaskName()); // currentTaskName = task1
}
Thread.sleep(100);
sw.stop();
StopWatch.TaskInfo lastTaskInfo = sw.getLastTaskInfo();
System.out.println("TimeSeconds = " + lastTaskInfo.getTimeSeconds()); // TimeSeconds = 0.1001189
System.out.println("LastTaskName = " + sw.getLastTaskName()); // LastTaskName = task1
System.out.println("LastTaskTimeMillis = " + sw.getLastTaskTimeMillis()); // LastTaskTimeMillis = 100
System.out.println("LastTaskTimeNanos = " + sw.getLastTaskTimeNanos()); // LastTaskTimeNanos = 100118900
sw.start("task2");
{
// do something
System.out.println("currentTaskName = " + sw.currentTaskName()); // currentTaskName = task2
System.out.println("isRunning = " + sw.isRunning()); // isRunning = true
}
Thread.sleep(200);
sw.stop();
System.out.println("currentTaskName = " + sw.currentTaskName()); // currentTaskName = null
System.out.println(sw.prettyPrint());
/*
StopWatch 'test': running time = 300009200 ns
---------------------------------------------
ns % Task name
---------------------------------------------
100146000 033% task1
199863200 067% task2
*/
System.out.println("shortSummary = " + sw.shortSummary()); // StopWatch 'test': running time = 300458499 ns
System.out.println("getTaskCount = " + sw.getTaskCount()); // 2
System.out.println("getTotalTimeSeconds = " + sw.getTotalTimeSeconds() + " s"); // 0.301075301 s
System.out.println("getTotalTimeMillis = " + sw.getTotalTimeMillis() + " ms"); // 300 ms
System.out.println("getTotalTimeNanos = " + sw.getTotalTimeNanos() + " ns"); // 301075301 ns
System.out.println("isRunning = " + sw.isRunning()); // false
System.out.println(sw); // StopWatch 'test': running time = 300361899 ns; [task1] took 99972100 ns = 33%; [task2] took 200389799 ns = 67%