类 StopWatch
Conceals use of System.nanoTime(), improving the readability of
application code and reducing the likelihood of calculation errors.
Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concept work and in development, rather than as part of production applications.
running time is tracked and reported in nanoseconds. the default time
unit for String renderings is seconds with decimal points in nanosecond
precision. Custom renderings with specific time units can be requested
through prettyPrint(TimeUnit).
- 从以下版本开始:
- 3.0
- 作者:
- Rod Johnson, Juergen Hoeller, Sam Brannen, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明static final classNested class to hold data about one task executed within theStopWatch. -
字段概要
字段修饰符和类型字段说明private StringName of the current task.private final StringIdentifier of thisStopWatch.private StopWatch.TaskInfoprivate longStart time of the current task.private intprivate ArrayList<StopWatch.TaskInfo>private longTotal running time. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明Get the name of the currently running task, if any.getId()Get the id of thisStopWatch, as specified on construction.Get the name of the last task.longGet the time taken by the last task in milliseconds.longGet the time taken by the last task in nanoseconds.intGet the number of tasks timed.Get an array of the data for tasks performed.doublegetTotalTime(TimeUnit timeUnit) Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).longGet the total time for all tasks in milliseconds.longGet the total time for all tasks in nanoseconds.doubleGet the total time for all tasks in seconds.booleanDetermine whether thisStopWatchis currently running.Get the last task as aStopWatch.TaskInfoobject.Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).prettyPrint(TimeUnit timeUnit) Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision).voidsetKeepTaskList(boolean keepTaskList) Configure whether theStopWatch.TaskInfoarray is built over time.Get a short description of the total running time in seconds.voidstart()Start an unnamed task.voidStart a named task.voidstop()Stop the current task.toString()Generate an informative string describing all tasks performed in seconds.
-
字段详细资料
-
id
Identifier of thisStopWatch.Handy when we have output from multiple stop watches and need to distinguish between them in log or console output.
-
taskList
-
startTimeNanos
private long startTimeNanosStart time of the current task. -
currentTaskName
Name of the current task. -
lastTaskInfo
-
taskCount
private int taskCount -
totalTimeNanos
private long totalTimeNanosTotal running time.
-
-
构造器详细资料
-
StopWatch
public StopWatch()Construct a newStopWatch.Does not start any task.
-
StopWatch
Construct a newStopWatchwith the given id.The id is handy when we have output from multiple stop watches and need to distinguish between them.
Does not start any task.
- 参数:
id- identifier for this stop watch
-
-
方法详细资料
-
getId
Get the id of thisStopWatch, as specified on construction.- 返回:
- the id (empty String by default)
- 另请参阅:
-
setKeepTaskList
public void setKeepTaskList(boolean keepTaskList) Configure whether theStopWatch.TaskInfoarray is built over time.Set this to
falsewhen using aStopWatchfor millions of tasks; otherwise, theTaskInfostructure will consume excessive memory.Default is
true. -
start
Start an unnamed task.The results are undefined if
stop()or timing methods are called without invoking this method first.- 抛出:
IllegalStateException- 另请参阅:
-
start
Start a named task.The results are undefined if
stop()or timing methods are called without invoking this method first.- 参数:
taskName- the name of the task to start- 抛出:
IllegalStateException- 另请参阅:
-
stop
Stop the current task.The results are undefined if timing methods are called without invoking at least one pair of
start()/stop()methods.- 抛出:
IllegalStateException- 另请参阅:
-
isRunning
public boolean isRunning()Determine whether thisStopWatchis currently running.- 另请参阅:
-
currentTaskName
Get the name of the currently running task, if any.- 另请参阅:
-
lastTaskInfo
Get the last task as aStopWatch.TaskInfoobject.- 抛出:
IllegalStateException- if no tasks have run yet
-
getLastTaskName
Get the name of the last task. -
getLastTaskTimeNanos
Get the time taken by the last task in nanoseconds. -
getLastTaskTimeMillis
Get the time taken by the last task in milliseconds. -
getTaskInfo
Get an array of the data for tasks performed.- 另请参阅:
-
getTaskCount
public int getTaskCount()Get the number of tasks timed. -
getTotalTimeNanos
public long getTotalTimeNanos()Get the total time for all tasks in nanoseconds.- 另请参阅:
-
getTotalTimeMillis
public long getTotalTimeMillis()Get the total time for all tasks in milliseconds.- 另请参阅:
-
getTotalTimeSeconds
public double getTotalTimeSeconds()Get the total time for all tasks in seconds.- 另请参阅:
-
getTotalTime
Get the total time for all tasks in the requested time unit (with decimal points in nanosecond precision).- 参数:
timeUnit- the unit to use- 另请参阅:
-
prettyPrint
Generate a table describing all tasks performed in seconds (with decimal points in nanosecond precision).For custom reporting, call
getTaskInfo()and use the data directly. -
prettyPrint
Generate a table describing all tasks performed in the requested time unit (with decimal points in nanosecond precision).For custom reporting, call
getTaskInfo()and use the data directly.- 参数:
timeUnit- the unit to use for rendering total time and task time- 另请参阅:
-
shortSummary
Get a short description of the total running time in seconds. -
toString
Generate an informative string describing all tasks performed in seconds.
-