Class Benchmark
The measurement is done by invoking begin()
and later calling end()
whichs returns the time
elapsed since the call to begin()
.
Notice that calls to begin()
and end()
can be nested, and each call to end()
refers to
the matching begin()
call. To ensure that all calls match, the preferred way to write a benchmark is
... Benchmark b = new Benchmark(); ... b.begin(); try { .... } finally { long ms = b.end(); }
This code layout also makes it visually easy to write correct pairs of begin()
/ end()
pairs.
The pair beginReporting()
and endReporting()
do basically the same, but report the benchmarking
information through an internal Benchmark.Reporter
object. The default Benchmark.Reporter
prints its messages by
System.out.println()
.
Reporting is only enabled if the Benchmark object was created through Benchmark(boolean)
with a
true
argument.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface used to report messages. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private final Benchmark.Reporter
private final boolean
-
Constructor Summary
ConstructorsConstructorDescriptionBenchmark
(boolean reportingEnabled) Benchmark
(boolean reportingEnabled, Benchmark.Reporter reporter) Sets up aBenchmark
with a customBenchmark.Reporter
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
begin()
void
Begins a benchmark (seebegin()
) and report the fact.void
beginReporting
(String message) Begins a benchmark (seebegin()
) and report the fact.long
end()
void
End a benchmark (seeend()
) and report the fact.void
endReporting
(String message) Ends a benchmark (seebegin()
) and report the fact.void
Reports the given message.void
Reports thetitle
, a colon, a space, and the pretty-printedObject
.private void
reportIndented
(String message) Reports a message throughreporter
, indent by N spaces where N is the current benchmark stack depth.
-
Field Details
-
beginTimes
-
reportingEnabled
private final boolean reportingEnabled -
reporter
-
PAD
- See Also:
-
-
Constructor Details
-
Benchmark
public Benchmark(boolean reportingEnabled) -
Benchmark
Sets up aBenchmark
with a customBenchmark.Reporter
.
-
-
Method Details
-
begin
public void begin()- See Also:
-
end
public long end()- See Also:
-
beginReporting
public void beginReporting()Begins a benchmark (seebegin()
) and report the fact. -
beginReporting
Begins a benchmark (seebegin()
) and report the fact. -
endReporting
public void endReporting()End a benchmark (seeend()
) and report the fact. -
endReporting
Ends a benchmark (seebegin()
) and report the fact. -
report
Reports the given message. -
report
Reports thetitle
, a colon, a space, and the pretty-printedObject
. -
reportIndented
Reports a message throughreporter
, indent by N spaces where N is the current benchmark stack depth.
-