java execution time

‮ptth‬s://www.lautturi.com
java execution time

In Java, you can measure the execution time of a block of code using the System.nanoTime() method. The System.nanoTime() method returns the current value of the most precise available system timer, in nanoseconds.

Here's an example of how to use the System.nanoTime() method to measure the execution time of a block of code in Java:

long startTime = System.nanoTime();

// code to be measured

long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("Execution time: " + duration + " nanoseconds");

In this example, the System.nanoTime() method is called before and after the block of code to be measured. The difference between the start time and the end time is calculated and printed as the execution time in nanoseconds.

You can use the System.nanoTime() method to measure the execution time of any block of code in Java. Just call the System.nanoTime() method before and after the code to be measured and calculate the difference between the start time and the end time.

Created Time:2017-11-03 15:57:13  Author:lautturi