2

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 Ienter image description here 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);
}

}

  • 2
    The execution time is so small that it the exec time of other pieces is destabilizing it. Run in a loop for 10K calls and divide time by 10K – Salim Apr 30 '20 at 18:49
  • You are running many programs at the same time. In reality, the operating system schedules programs to run for a short interval of time (called a "slice") and then preempts the program and schedules another to run. On modern systems, you might have multiple cores (which means actually multiple things do run at the same time, but only one per core - the rest is time slicing). You can't reliably measure what you want without booting to a single user single tasking state. And even then, I don't know what you gain other than knowledge about the "optimal case" on one core of your particular system. – Elliott Frisch Apr 30 '20 at 18:52
  • Also, you aren't giving the JIT a chance to kick-in. So this measurement is unlikely to be the "optimal case". – Elliott Frisch Apr 30 '20 at 18:53
  • Micro-benchmarking like this is generally a waste of time. As with other Micro-benchmarking testing, Math.sqrt will probably perform differently under different conditions – ControlAltDel Apr 30 '20 at 18:54
  • @ElliottFrisch Please tell me how to proceed further. Is there any other way to measure the time in nanoseconds or not? Actually I have written a square root function by myself and I want to compare my own square root function with Math.sqrt() in terms of execution time taken by them. – shubham1355 Apr 30 '20 at 19:01
  • @shubham1355 Probably not. You need to run this ***many*** times and then average it. Not try and measure the speed of executing it once. It probably won't ever run the exact same speed twice. The resolution of the system clock is not that granular. Quartz crystals (for example) normally oscillate 32768 times per second. That's nowhere near nanosecond precision. And even they, precise as they are, exhibit something called "jitter" (teeny-tiny little errors that usually average out - but, as wikipedia puts it, *a typical quartz clock or wristwatch will gain or lose 15 seconds per 30 days*). – Elliott Frisch Apr 30 '20 at 19:08
  • * Quote from [Quartz clock](https://en.wikipedia.org/wiki/Quartz_clock). See also the [`time(7)` man page](http://man7.org/linux/man-pages/man7/time.7.html), *On a system that supports HRTs, the accuracy of sleep and timer system calls is no longer constrained by the jiffy, but instead can be as accurate as the hardware allows (microsecond accuracy is typical of modern hardware).* – Elliott Frisch Apr 30 '20 at 19:10
  • Does this answer your question? [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Ole V.V. May 02 '20 at 05:33

0 Answers0