How do I calculate process execution time in higher resolution?

This example demonstrates how to use the System.nanoTime() method to calculate processing execution time in higher resolution. The processing time calculated in nanoseconds resolution.

package org.kodejava.lang;

public class NanoSecondsTimerResolution {
    public static void main(String[] args) {
        // Get process execution start time in nanoseconds.
        long start = System.nanoTime();
        System.out.println("Process start... " + start);

        try {
            Thread.sleep(5000); // Simulate a long process.
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Get process execution finish time in nanoseconds.
        long finish = System.nanoTime();
        System.out.println("Process finish... " + finish);

        // Calculate the process execution time.
        long execTime = finish - start;
        System.out.println("Processing time = " + execTime + "(ns)");
    }
}

How do I read system property as an integer?

The Integer.getInteger() methods allows us to easily read system property and convert it directly to Integer object. This method call the System.getProperty() method and then convert it to integer by calling the Integer.decode() method.

package org.kodejava.lang;

public class IntegerProperty {
    public static void main(String[] args) {
        // Add properties to the system. In this example we create a
        // dummy major and minor version for our application.
        System.setProperty("app.major.version", "2021");
        System.setProperty("app.minor.version", "9");

        // In the code below we use the Integer.getInteger() method to
        // read our application version from the value specified in the
        // system properties.
        Integer major = Integer.getInteger("app.major.version");
        Integer minor = Integer.getInteger("app.minor.version");
        System.out.println("App version = " + major + "." + minor);
    }
}

The output of the code snippet:

App version = 2021.9

How do I calculate process elapsed time?

This example shows us how to use the System.nanoTime() method to get the amount of time a process take place. Please be aware that the nano time value is not related to the real world time value.

package org.kodejava.lang;

public class ElapsedTimeExample {
    public static void main(String[] args) {
        // Get the start time of the process
        long start = System.nanoTime();
        System.out.println("Start: " + start);

        // Just do some a bit long process calculating the total value
        // of even number from zero to 10000
        int totalEven = 0;
        for (int i = 0; i < 10000; i++) {
            if (i % 2 == 0) {
                totalEven = totalEven + i;
            }
        }

        // Get the end time of the process
        long end = System.nanoTime();
        System.out.println("End  : " + end);

        long elapsedTime = end - start;

        // Show how long it took to finish the process
        System.out.println("The process took approximately: "
            + elapsedTime + " nano seconds");
    }
}

And example of the result are:

Start: 35034476484699
End  : 35034477434200
The process took approximately: 949501 nano seconds

How can I get current working directory?

A system properties named user.dir can be used if you want to find the current working directory of your Java program.

package org.kodejava.io;

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // System property key to get current working directory.
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);
    }
}

Result example:

Working Directory: F:\Wayan\Kodejava\kodejava-example