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
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.