patternjavaMinor
Measuring performance of code using StopWatch
Viewed 0 times
stopwatchusingperformancemeasuringcode
Problem
I have created a StopWatch class to measure the performance of any code. I use this StopWatch code in any of my multithreading projects as well.
```
public static class StopWatch {
public static ConcurrentHashMap histogram = new ConcurrentHashMap();
/**
* Creates an instance of the timer and starts it running.
*/
public static StopWatch getInstance() {
return new StopWatch();
}
private long end = -1;
private long interval = -1;
private final long start;
private StopWatch() {
start = interval = currentTime();
}
/**
* Returns in milliseconds the amount of time that has elapsed since the timer was created. If the
* stop method has been invoked, then this returns instead the elapsed time between the creation of
* the timer and the moment when stop was invoked.
*
* @return duration it took
*/
public long getDuration() {
long result = 0;
final long startTime = start;
final long endTime = isTimerRunning() ? currentTime() : end;
result = nanoToMilliseconds(endTime - startTime);
boolean done = false;
while (!done) {
Long oldValue = histogram.putIfAbsent(result, 1L);
if (oldValue != null) {
done = histogram.replace(result, oldValue, oldValue + 1);
} else {
done = true;
}
}
return result;
}
/**
* Returns in milliseconds the amount of time that has elapsed since the last invocation of this same method. If
* this method has not previously been invoked, then it is the amount of time that has elapsed since the timer
* was created. Note that once the stop method has been invoked this will just
* return zero.
*
* @return interval period
*/
public long getInterval() {
long result = 0;
final long startTime = interval;
final long endTime;
if (isTimerRunn
```
public static class StopWatch {
public static ConcurrentHashMap histogram = new ConcurrentHashMap();
/**
* Creates an instance of the timer and starts it running.
*/
public static StopWatch getInstance() {
return new StopWatch();
}
private long end = -1;
private long interval = -1;
private final long start;
private StopWatch() {
start = interval = currentTime();
}
/**
* Returns in milliseconds the amount of time that has elapsed since the timer was created. If the
* stop method has been invoked, then this returns instead the elapsed time between the creation of
* the timer and the moment when stop was invoked.
*
* @return duration it took
*/
public long getDuration() {
long result = 0;
final long startTime = start;
final long endTime = isTimerRunning() ? currentTime() : end;
result = nanoToMilliseconds(endTime - startTime);
boolean done = false;
while (!done) {
Long oldValue = histogram.putIfAbsent(result, 1L);
if (oldValue != null) {
done = histogram.replace(result, oldValue, oldValue + 1);
} else {
done = true;
}
}
return result;
}
/**
* Returns in milliseconds the amount of time that has elapsed since the last invocation of this same method. If
* this method has not previously been invoked, then it is the amount of time that has elapsed since the timer
* was created. Note that once the stop method has been invoked this will just
* return zero.
*
* @return interval period
*/
public long getInterval() {
long result = 0;
final long startTime = interval;
final long endTime;
if (isTimerRunn
Solution
Just a few little things:
-
A public static
-
Many of your comments are unnecessary. For example the comment before
-
The
This saves you two lines of code and makes the code clearer. The same applies to
-
by using the ternary if operator, you can shorten
The assignment
-
A public static
getInstance() method is commonly used in the singleton pattern. However, you always create a new StopWatch. At least you should name it to createNewInstance(), if you want to keep the method at all.-
Many of your comments are unnecessary. For example the comment before
isTimerRunning() doesn't help to understand the code. If you are generating documentation, they might be required, if not, omit such comments.-
The
result variable in getInterval() is not required. You can just return the result like this:return nanoToMilliseconds(endTime - startTime);This saves you two lines of code and makes the code clearer. The same applies to
getDuration().-
by using the ternary if operator, you can shorten
getInterval() a bit more:public long getInterval() {
final long startTime = interval;
final long endTime = isTimerRunning() ? currentTime() : end;
interval = endTime;
return nanoToMilliseconds(endTime - startTime);;
}The assignment
interval = endTime now also happens when the StopWatch has already been stopped but this isn't a problem.Code Snippets
return nanoToMilliseconds(endTime - startTime);public long getInterval() {
final long startTime = interval;
final long endTime = isTimerRunning() ? currentTime() : end;
interval = endTime;
return nanoToMilliseconds(endTime - startTime);;
}Context
StackExchange Code Review Q#114718, answer score: 7
Revisions (0)
No revisions yet.