Time an application in Java

problem

You have developed two or more solutions for a given problem and in order to decide which one to keep you wish to find out which one is faster. This post is about using the time tool to show a quick time measurement of a given program’s execution.

SOLUTION

Let’s suppose the problem is a Java program that adds 10 000 strings together. For example one solution is simply using the + operator as follows:

ConcatenateStrings.java
				
					package com.programmerabroad;

public class ConcatenateStrings {

    public String addRepeated(String text, int iterations) {

        String concatenated = "";

        for(int i = 0; i < iterations; i++) {
            concatenated += text;
        }

        return concatenated;
    }

    public static void main(String[] args) {
        new ConcatenateStrings().addRepeated("-test-", 10_000);
    }
}
				
			

The second solution uses a StringBuilder to accomplish the same result. The code is as follows:

StringBuilderConcatenator.java
				
					package com.programmerabroad;

public class StringBuilderConcatenator {

    public String addRepeated(String text, int iterations) {

        StringBuilder concatenated = new StringBuilder();

        for (int i = 0; i < iterations; i++) {
            concatenated.append(text);
        }

        return concatenated.toString();
    }

    public static void main(String[] args) {
        new StringBuilderConcatenator().addRepeated("-test-", 10_000);
    }
}
				
			
running

Ok, now that we have two solutions let’s compile them. They exist in the same directory, so they can simply be compiled at once:

				
					javac com/programmerabroad/*.java
				
			

Great, the two programs are now compiled and ready to be executed as follows:

  • java com.programmerabroad.ConcatenateStrings
  • java com.programmerabroad.StringBuilderConcatenator

Because we want to compare their execution time, instead of simply running the program, we can prepend the time tool that is shipped on *nix/ Mac Operating Systems.

Example:

				
					time application_to_measure_its_time
				
			

In this case:

				
					time java com.programmerabroad.ConcatenateStrings
				
			

and for the second program

				
					time java com.programmerabroad.StringBuilderConcatenator
				
			

Or running both of them at the same time:

				
					time java com.programmerabroad.ConcatenateStrings && time java com.programmerabroad.StringBuilderConcatenator
				
			
output

The output would be:

				
					java com.programmerabroad.ConcatenateStrings  0.33s user 0.16s system 97% cpu 0.506 total
java com.programmerabroad.StringBuilderConcatenator  0.07s user 0.03s system 83% cpu 0.111 total
				
			

As you can see the second solution is by far much faster! More specific, using the StringBuilder to concatenate strings is 4.7x times faster than the simple + operator.

conclusion

n this post we saw how the time tool can be used to assist us in choosing between two solutions. It can be used with any program and not only Java. This was one simple way to measure execution time of a program executed in the terminal. It could have be done inside the Java code but that’s more work and it should have been repeated across all the candidate solutions. The time tool is simply handy in this case.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x