Calculate Timings using Commons Lang StopWatch

You need to calculate timings of your code execution so you know how long a particular method or some block in your code take to finish it execution. Basically you can do this by capturing the start time and the end time usingĀ System.currentTimeMillis() and find the different. Another way to do it is to use the StopWatch class from Apache Commons Lang library. The StopWatch class can be found in theĀ org.apache.commons.lang.time package.

The simplest steps to use the StopWatch is to create an instance of the StopWatch class, start the stopwatch by calling the start() method. After the stopwatch is started you can execute the target method or block of code you want to watch and call the stop() method to complete the timing session. To get the time of the stopwatch you can call the getTime() method.

Now, let’s see the code for the process described above.

Here is the output of the code above:

Beside doing a simple timing calculation using the start() and stop() followed by the getTime() methods, the StopWatch class also provides methods for splitting the time, suspend and resuming the stopwatch. You can use the split(), suspend() and resume() method respectively. To get the split time you can call the toSplitString() method.

The code snippet above will output something like this:

Another method that you can find in the StopWatch class is the getStartTime() which will return the stopwatch start time. The reset() method will reset the stopwatch. To remove a split you can call the unsplit method.

How do I clone an array variable?

You have an array variable and you want to make a clone of this array into a new array variable. To do this you can use Apache Commons Lang ArrayUtils.clone() method. The code snippet below demonstrates the cloning of a primitive array that contains some integers elements in it.

The fibonacci array contents were cloned into the clone array and we print out the content using ArrayUtils.toString() method.

In the code snippet above the clone() method create a reference to a new array. The clone() method it self doesn’t not change the original array. In addition to clone primitive arrays the clone() method also work for cloning array of objects.

As an example we will create an array of String objects and clone it using the ArrayUtils.clone() method. To display the contents of the array we will again use the ArrayUtils.toString() method.

And here is the result:

The only different from the when cloning a primitive array and object array using the ArrayUtils.clone() method is that when cloning an object such as String, Date, etc we need to cast the result of the clone() method into the targeted object type.

How do I print the contents of an array variable?

You need to print the contents of an array variable. The long way to it is to user a loop to print each element of the array. To simplify this you can user Apache Commons Lang ArrayUtils.toString() method. This method can takes any array as a parameter and print out the contents separated by commas and surrounded by curly brackets. When you need to print a specific string when the array is null, you can provide the second string argument to this method.

The output of the code snippet above:

If you are using the JDK 1.5 or later you can actually use the java.util.Arrays class to do the same thing as the org.apache.commons.lang.ArrayUtils class does.