I just want to find out the exact execution time of java math functions like Math.sqrt() in the nanosecond range. I have used System.nanoTime() to find the time but it's giving different execution time on each run.
Below is the code that I am using"
import java.util.concurrent.TimeUnit;
import java.lang.Math;
class TimeUtil
{
public static void main(String[] args) throws InterruptedException {
long startTime = System.nanoTime();
/* ... the code being measured starts ... */
Math.sqrt(5.625);
/* ... the code being measured ends ... */
long endTime = System.nanoTime();
// get difference of two nanoTime values
long timeElapsed = endTime - startTime;
System.out.println("Execution time in nanoseconds : " + timeElapsed);
System.out.println("Execution time in milliseconds : " +
timeElapsed / 1000000);
}
}