Java:性能 SQRT 计算

标签 java performance math sqrt

我有这个代码:

package math;

import java.io.IOException;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        System.out.println("Hi, I will beat Java's Math.sqrt(double) method");
        System.out.println("Both ways of calculation will be done");
        System.out.println("I will time how long they took to calculate");
        System.out.println("Random doubles will be generated");
        System.out.println();
        System.out.println("Please give the number of sqrt-calculation will be done");
        int calcs = new Scanner(System.in).nextInt();
        boolean output = true;
        if (calcs > 10000)
        {
            System.out.println("You're asking much calculations");
            System.out.println("Disabling output is recommend");
            System.out.println("Disable output? (y/n)");
            char a = (char) System.in.read();
            if (a == 'y')
            {
                output = false;
            }
        }
        System.out.println("Press enter to start");
        System.in.read();
        test(calcs, output);
        System.out.println();
        System.out.println("I was much faster I think");
        System.out.println("Now you can check my precision");
        System.out.println("Please give a complex double");
        double x = Double.parseDouble(new Scanner(System.in).next());
        System.out.println();
        System.out.println("Math.sqrt(" + x + ")           = " + Math.sqrt(x));
        System.out.println("SqrtCalculator.sqrt(" + x + ") = " + sqrt(x));
        System.out.println("------------------------");
        System.out.println("Now please make your conclusion");
        System.out.println("Thanks for trying");
    }

    public static void test(int calculations, boolean output)
    {
        double factor = Math.random() / 2;
        // Math
        long mathStart = System.currentTimeMillis();
        for (int i = 1; i <= calculations; i++)
        {
            double x = i * factor;
            double result = Math.sqrt(x);
            if (output)
            {
                System.out.println("Math.sqrt(" + x + ") =  " + result);
            }
        }
        long mathStop = System.currentTimeMillis();
        long mathTime = mathStop - mathStart;
        // My Method
        long myStart = System.currentTimeMillis();
        for (int i = 1; i <= calculations; i++)
        {
            double x = i * factor;
            double result = sqrt(x);
            if (output)
            {
                System.out.println("SqrtCalculater.sqrt(" + x + ") =  " + result);
            }
        }
        long myStop = System.currentTimeMillis();
        long myTime = myStop - myStart;
        System.out.println();
        if (output)
            System.out.println("---------------------------");
        System.out.println("Here are the results:");
        System.out.println("Math and SqrtCalculator did each " + calculations + " of the same sqrt-calculations");
        System.out.println();
        System.out.println("Math: " + mathTime + " milliseconds");
        System.out.println("I:    " + myTime + " milliseconds");
    }

    public final static double sqrt(double x)
    {
        double previous = 1;
        double now = 0;
        for (;;)
        {
            now = (x / previous + previous) / 2;
            if (previous == now)
            {
                return now;
            }
            previous = now;
        }
    }
}

这个 sqrt 方法叫做“heroon”。
如果我运行我的程序并询问 80000 次计算并禁用输出,则 Math.sqrt() 比我的方法快得多。如果我要求 80000 次计算并启用输出,我的方法会快得多。

谁能解释一下?

谢谢

抱歉英语不好。

最佳答案

我无法重现您的结果。使用 Eclipse Galileo 和 JDK 1.6.0 尝试了一些时间。

对于 80000,禁用输出,我得到类似的东西:

Math: 15 milliseconds
I:    32 milliseconds

小的时候,最好用System.nanoTime()或者更多的交互。

对于 80000,启用输出:

Math: 3609 milliseconds
I:    4906 milliseconds

所以问题可能出在处理输出的方式上(滚动、缓冲……)

关于Java:性能 SQRT 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1995457/

相关文章:

c - 理解代码背后的数学原理

c# - 道格拉斯-普克算法 : understanding use with polgyons

java - 如何在selenium和testng中打印异常

java - 输入字段更改后的 Wicket 数据表过滤

java - Java 中的 JScrollPane

c# - Web API 的性能

python - Numpy 二维数组 : change all values to the right of NaNs

math - 如何计算椭圆圆周上的一个点?

java - Log4j2 - 未找到 Spring 测试记录器

javascript - 由于第三方分析脚本,加快了移动设备上加载缓慢的网站速度