public final class Timer extends Object
System.nanoTime()
for more
details). The recorded times themselves are not preserved,
however, only the statistics. As a consequence start()
and stop()
can be called many number of times
without requiring large amounts of memory.
When size() == 0
,
min() == Double.NaN max() == Double.NaN mean() == Double.NaN standardDeviation() == Double.NaN
When size() == 1
,
standardDeviation() == 0.0d
Execution time can be sensitive to various factors, such as order of execution, runtime optimization from the just-in-time compiler (JIT), and garbage collection. This class provides static methods to help deal with these factors.
Given a few benchmarks to run, wrap them in Runnable objects
Runnable r0 = new Runnable() { public void run() { ... } }; Runnable r1 = new Runnable() { public void run() { ... } }; Runnable r2 = new Runnable() { public void run() { ... } }; List<Runnable> benchmarks = Arrays.asList(new Runnable[] { r0, r1, r2 });Prime the JIT by running the benchmarks several times
Timer.prime(benchmarks, 1000);Then measure the execution times of the benchmarks by running them several times in random execution order
Map<Runnable, Timer> result = Timer.shuffle(benchmarks, 100, 100);Summary statistics on recorded execution times are captured by the timer returned for each Runnable benchmark
for (Map.Entry<Runnable, Timer> e : result.entrySet()) { Runnable r = e.getKey(); Timer t = e.getValue(); System.out.println("runnable=" + r + " mean execution time=" + t.mean() + "ns"); }
System.nanoTime()
Constructor and Description |
---|
Timer()
Create a new timer.
|
Modifier and Type | Method and Description |
---|---|
static Map<Runnable,Timer> |
loop(List<? extends Runnable> codeBlocks,
int n)
For each of the code blocks in the specified list of code blocks,
loop over the code block
n times. |
static Map<Runnable,Timer> |
loop(List<? extends Runnable> codeBlocks,
int n,
int m)
Loop over the code blocks in the specified list of code blocks
n times, executing each code block m times. |
static Timer |
loop(Runnable codeBlock,
int n)
Loop over the specified code block
n times. |
static Timer |
loop(Runnable codeBlock,
int n,
Timer t)
Loop over the specified code block
n times
with the specified timer. |
double |
max()
Return the maximum elapsed time recorded by this timer
in nanoseconds.
|
double |
mean()
Return the arithmetic mean of the elapsed times recorded by this
timer in nanoseconds.
|
double |
min()
Return the minimum elapsed time recorded by this timer
in nanoseconds.
|
static void |
prime(List<? extends Runnable> codeBlocks,
int n)
Prime the just-in-time compiler (JIT) by executing each
code block in the specified list of code blocks
n times. |
static void |
prime(Runnable codeBlock,
int n)
Prime the just-in-time compiler (JIT) by executing the
specified code block
n times. |
void |
reset()
Reset the record of elapsed times from this timer.
|
static Map<Runnable,Timer> |
shuffle(List<? extends Runnable> codeBlocks,
int n)
For each of the code blocks in the specified list of code blocks,
executed in random order, loop over the code block
n times. |
static Map<Runnable,Timer> |
shuffle(List<? extends Runnable> codeBlocks,
int n,
int m)
Loop over the code blocks in the specified list of code blocks
n times, in random order, executing each code block
m times. |
static Map<Runnable,Timer> |
shuffle(List<? extends Runnable> codeBlocks,
int n,
int m,
Random random)
Loop over the code blocks in the specified list of code blocks
n times, in random order using the specified source of
randomness, executing each code block m times. |
static Map<Runnable,Timer> |
shuffle(List<? extends Runnable> codeBlocks,
int n,
Random random)
For each of the code blocks in the specified list of code blocks,
executed in random order using the specified source of randomness,
loop over the code block
n times. |
long |
size()
Return the number of elapsed times recorded by this timer.
|
double |
standardDeviation()
Return the standard deviation of the elapsed times recorded by this
timer in nanoseconds.
|
void |
start()
Start the timer.
|
void |
stop()
Stop the timer and record the time elapsed in nanoseconds
since
start() was last called. |
double |
sum()
Return the sum of the elapsed times recorded by this timer
in nanoseconds.
|
static Timer |
time(Runnable codeBlock)
Time the specified code block.
|
static Timer |
time(Runnable codeBlock,
Timer t)
Time the specified code block with the specified timer.
|
public void reset()
stop()
is called.public void start()
stop()
is called.public void stop()
start()
was last called.TimerException
- if start()
has not been called
at least once before this method was calledpublic double min()
public double max()
public long size()
public double sum()
public double mean()
public double standardDeviation()
public static Timer time(Runnable codeBlock)
codeBlock
- code block to executepublic static Timer time(Runnable codeBlock, Timer t)
codeBlock
- code block to executet
- timer to use to measure execution timepublic static void prime(Runnable codeBlock, int n)
n
times.codeBlock
- code block to executen
- number of times to execute code blockpublic static void prime(List<? extends Runnable> codeBlocks, int n)
n
times.codeBlocks
- list of code blocks to executen
- number of times to execute each code blockpublic static Timer loop(Runnable codeBlock, int n)
n
times.codeBlock
- code block to executen
- number of times to execute code blockpublic static Timer loop(Runnable codeBlock, int n, Timer t)
n
times
with the specified timer.codeBlock
- code block to executen
- number of times to execute code blockt
- timer to use to measure execution timepublic static Map<Runnable,Timer> loop(List<? extends Runnable> codeBlocks, int n)
n
times.codeBlocks
- list of code blocks to executen
- number of times to execute each code blockpublic static Map<Runnable,Timer> loop(List<? extends Runnable> codeBlocks, int n, int m)
n
times, executing each code block m
times.codeBlocks
- list of code blocks to executen
- number of times to loop over the list of code blocksm
- number of times to execute each code blockpublic static Map<Runnable,Timer> shuffle(List<? extends Runnable> codeBlocks, int n)
n
times.codeBlocks
- list of code blocks to executen
- number of times to execute each code blockpublic static Map<Runnable,Timer> shuffle(List<? extends Runnable> codeBlocks, int n, Random random)
n
times.codeBlocks
- list of code blocks to executen
- number of times to execute each code blockrandom
- source of randomnesspublic static Map<Runnable,Timer> shuffle(List<? extends Runnable> codeBlocks, int n, int m)
n
times, in random order, executing each code block
m
times.codeBlocks
- list of code blocks to executen
- number of times to loop over the list of code blocksm
- number of times to execute each code blockpublic static Map<Runnable,Timer> shuffle(List<? extends Runnable> codeBlocks, int n, int m, Random random)
n
times, in random order using the specified source of
randomness, executing each code block m
times.codeBlocks
- list of code blocks to executen
- number of times to loop over the list of code blocksm
- number of times to execute each code blockrandom
- source of randomnessCopyright (c) 2004-2013 held jointly by the individual authors. Licensed under the GNU Lesser General Public License (LGPL).