首页 > 解决方案 > 如何在纳秒范围内找到 java 数学库函数的确切执行时间?

问题描述

我只想找出像 Math.sqrt() 这样的 java 数学函数在纳秒范围内的确切执行时间。我已经使用 System.nanoTime() 来查找时间,但每次运行它给出不同的执行时间。

下面是我在此处输入图像描述正在使用的代码“

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);
}

}

标签: javaperformancetime

解决方案


推荐阅读