Java 为什么我的程序有一个较长的运行时间,而它应该有一个较短的运行时间?

Java 为什么我的程序有一个较长的运行时间,而它应该有一个较短的运行时间?,java,matrix,iteration,fibonacci,exponentiation,Java,Matrix,Iteration,Fibonacci,Exponentiation,我有两个程序来计算用户指定的斐波那契序列中的第n项。 第一个程序是这样的: import java.util.Scanner; import java.text.DecimalFormat; import java.math.BigInteger; import java.io.BufferedWriter; import java.io.FileWriter; public class Fibonacci { // main(String[]) - Bulk of the progr

我有两个程序来计算用户指定的斐波那契序列中的第n项。 第一个程序是这样的:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.math.BigInteger;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Fibonacci
{
    // main(String[]) - Bulk of the program
    public static void main(String[] args)
    {
        long lStartTime;
        long lFinishTime;
        long lTotalTime;
        long l;
        long lInput = 0L;
        long lToGoTo = 0L;
        String strInput;
        Scanner keyboard = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#,##0");
        BigInteger biNMinusOne = BigInteger.ZERO;
        BigInteger biN = BigInteger.ONE;
        BigInteger biHeld;
        FileWriter fw;
        BufferedWriter bw;

        try
        {
            System.out.print("\nEnter which term of the Fibonacci Sequence you would like: ");
            lInput = keyboard.nextLong();
            lToGoTo = lInput - 1L;
        }
        catch (Exception e)
        {
            System.exit(0);
        }

        lStartTime = System.currentTimeMillis();
        if(lInput != 0)
        {
            for(l = 0; l < lToGoTo; l++)
            {
                biHeld = biNMinusOne.add(biN);
                biNMinusOne = biN;
                biN = biHeld;
                System.out.print(l + "\n");
            }
        }
        else
        {
            biN = BigInteger.ZERO;
        }
        System.out.print(lInput + "\n");
        lFinishTime = System.currentTimeMillis();
        lTotalTime = lFinishTime - lStartTime;
        System.out.print("\nTotal Computation Time: " + lTotalTime + "ms\n");

        try
        {
            fw = new FileWriter("Fibonacci.txt");
            bw = new BufferedWriter(fw);
            bw.write(df.format(biN).toString());
            bw.close();
            System.out.print("\nSee \"Fibonacci.txt\" to see the result.\n");
        }
        catch (Exception e)
        {
            System.out.print("\nError!\n");
        }
    }// End main(String[])
}//end Fibonacci
import java.util.Scanner;
import java.text.DecimalFormat;
import java.math.BigInteger;
import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 * Documentation:
 * Fibonacci Identity A^n = n+1 n
 *                          n n-1
 * where for n = 1    A^1 = 1   1
 *                          1   0
 * and/or where n corresponds to the nth Fibonacci term
 * and where if the user inputs n, the n+1 term in the n-1 exponentiation of A will
 * be the requested term
 */
public class Fibonacci
{
    // main(String[]) - Bulk of the program
    public static void main(String[] args)
    {
        long l;
        long lStart;
        long lFinish;
        long lInput = 0L;
        long lTerm = 0L;
        BigInteger biN = BigInteger.ZERO;
        BigInteger[][] rgbiN  = new BigInteger[2][2];
        BigInteger[][] rgbiFibonacci = {{BigInteger.ONE, BigInteger.ONE}, 
                                       {BigInteger.ONE, BigInteger.ZERO}};
        Scanner keyboard = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#,##0");
        FileWriter fw;
        BufferedWriter bw;

        try
        {
            System.out.print("\nEnter which term of the Fibonacci Sequence you would like: ");
            lInput = keyboard.nextLong();
            lTerm = lInput - 2;
        }
        catch (Exception e)
        {
            System.exit(0);
        }

        lStart = System.currentTimeMillis();
        rgbiN = rgbiFibonacci;
        if(lTerm != -1)
        {
            for(l = 0; l < lTerm; l++)
            {
                rgbiN = multiplyMatrix(rgbiN);
                System.out.print(l + "\n");
            }
            biN = rgbiN[0][0];
            System.out.print(l + "\n");
        }
        lFinish = System.currentTimeMillis() - lStart;
        System.out.print("\nTotal Computation Time: " + lFinish + "ms\n");

        try
        {
            fw = new FileWriter("Fibonacci.txt");
            bw = new BufferedWriter(fw);
            bw.write(df.format(biN).toString());
            bw.close();
            System.out.print("\nSee \"Fibonacci.txt\" to see the result.\n");
        }
        catch (Exception e)
        {
            System.out.print("\nError!\n");
        }
    }// End main(String[])

    public static BigInteger[][] multiplyMatrix(BigInteger[][] n)
    {
        BigInteger biA;
        BigInteger biB;
        BigInteger biC;
        BigInteger biD;
        BigInteger[][] rgbiN = new BigInteger[2][2];
        BigInteger[][] rgbiFibonacci = {{BigInteger.ONE, BigInteger.ONE}, 
                                       {BigInteger.ONE, BigInteger.ZERO}};
        biA = ((n[0][0].multiply(rgbiFibonacci[0][0])).add(n[0][1].multiply(rgbiFibonacci[1][0])));
        biB = ((n[0][0].multiply(rgbiFibonacci[0][1])).add(n[0][1].multiply(rgbiFibonacci[1][1])));
        biC = ((n[1][0].multiply(rgbiFibonacci[0][0])).add(n[1][1].multiply(rgbiFibonacci[1][0])));
        biD = ((n[1][0].multiply(rgbiFibonacci[0][1])).add(n[1][1].multiply(rgbiFibonacci[1][1])));

        rgbiN[0][0] = biA;
        rgbiN[0][1] = biB;
        rgbiN[1][0] = biC;
        rgbiN[1][1] = biD;

        return (rgbiN);
    }//end multiplyMatrix(int[][], int[][])
}//end Fibonacci
import java.util.Scanner;
导入java.text.DecimalFormat;
导入java.math.biginger;
导入java.io.BufferedWriter;
导入java.io.FileWriter;
公共类斐波那契
{
//main(字符串[])-程序的大部分
公共静态void main(字符串[]args)
{
第一时间长;
完成时间长;
时间长;
长l;
长输入=0L;
长lToGoTo=0升;
弦纹;
扫描仪键盘=新扫描仪(System.in);
DecimalFormat df=新的DecimalFormat(“#,##0”);
BigInteger binminsune=BigInteger.0;
biginger biN=biginger.ONE;
大整数;
文件编写器;
缓冲写入器bw;
尝试
{
System.out.print(“\n输入您想要的斐波那契序列的哪个项:”;
lInput=keyboard.nextLong();
lToGoTo=lInput-1L;
}
捕获(例外e)
{
系统出口(0);
}
lStartTime=System.currentTimeMillis();
if(lInput!=0)
{
对于(l=0;l
并通过迭代计算第n项

我的第二个计划是:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.math.BigInteger;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Fibonacci
{
    // main(String[]) - Bulk of the program
    public static void main(String[] args)
    {
        long lStartTime;
        long lFinishTime;
        long lTotalTime;
        long l;
        long lInput = 0L;
        long lToGoTo = 0L;
        String strInput;
        Scanner keyboard = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#,##0");
        BigInteger biNMinusOne = BigInteger.ZERO;
        BigInteger biN = BigInteger.ONE;
        BigInteger biHeld;
        FileWriter fw;
        BufferedWriter bw;

        try
        {
            System.out.print("\nEnter which term of the Fibonacci Sequence you would like: ");
            lInput = keyboard.nextLong();
            lToGoTo = lInput - 1L;
        }
        catch (Exception e)
        {
            System.exit(0);
        }

        lStartTime = System.currentTimeMillis();
        if(lInput != 0)
        {
            for(l = 0; l < lToGoTo; l++)
            {
                biHeld = biNMinusOne.add(biN);
                biNMinusOne = biN;
                biN = biHeld;
                System.out.print(l + "\n");
            }
        }
        else
        {
            biN = BigInteger.ZERO;
        }
        System.out.print(lInput + "\n");
        lFinishTime = System.currentTimeMillis();
        lTotalTime = lFinishTime - lStartTime;
        System.out.print("\nTotal Computation Time: " + lTotalTime + "ms\n");

        try
        {
            fw = new FileWriter("Fibonacci.txt");
            bw = new BufferedWriter(fw);
            bw.write(df.format(biN).toString());
            bw.close();
            System.out.print("\nSee \"Fibonacci.txt\" to see the result.\n");
        }
        catch (Exception e)
        {
            System.out.print("\nError!\n");
        }
    }// End main(String[])
}//end Fibonacci
import java.util.Scanner;
import java.text.DecimalFormat;
import java.math.BigInteger;
import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 * Documentation:
 * Fibonacci Identity A^n = n+1 n
 *                          n n-1
 * where for n = 1    A^1 = 1   1
 *                          1   0
 * and/or where n corresponds to the nth Fibonacci term
 * and where if the user inputs n, the n+1 term in the n-1 exponentiation of A will
 * be the requested term
 */
public class Fibonacci
{
    // main(String[]) - Bulk of the program
    public static void main(String[] args)
    {
        long l;
        long lStart;
        long lFinish;
        long lInput = 0L;
        long lTerm = 0L;
        BigInteger biN = BigInteger.ZERO;
        BigInteger[][] rgbiN  = new BigInteger[2][2];
        BigInteger[][] rgbiFibonacci = {{BigInteger.ONE, BigInteger.ONE}, 
                                       {BigInteger.ONE, BigInteger.ZERO}};
        Scanner keyboard = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#,##0");
        FileWriter fw;
        BufferedWriter bw;

        try
        {
            System.out.print("\nEnter which term of the Fibonacci Sequence you would like: ");
            lInput = keyboard.nextLong();
            lTerm = lInput - 2;
        }
        catch (Exception e)
        {
            System.exit(0);
        }

        lStart = System.currentTimeMillis();
        rgbiN = rgbiFibonacci;
        if(lTerm != -1)
        {
            for(l = 0; l < lTerm; l++)
            {
                rgbiN = multiplyMatrix(rgbiN);
                System.out.print(l + "\n");
            }
            biN = rgbiN[0][0];
            System.out.print(l + "\n");
        }
        lFinish = System.currentTimeMillis() - lStart;
        System.out.print("\nTotal Computation Time: " + lFinish + "ms\n");

        try
        {
            fw = new FileWriter("Fibonacci.txt");
            bw = new BufferedWriter(fw);
            bw.write(df.format(biN).toString());
            bw.close();
            System.out.print("\nSee \"Fibonacci.txt\" to see the result.\n");
        }
        catch (Exception e)
        {
            System.out.print("\nError!\n");
        }
    }// End main(String[])

    public static BigInteger[][] multiplyMatrix(BigInteger[][] n)
    {
        BigInteger biA;
        BigInteger biB;
        BigInteger biC;
        BigInteger biD;
        BigInteger[][] rgbiN = new BigInteger[2][2];
        BigInteger[][] rgbiFibonacci = {{BigInteger.ONE, BigInteger.ONE}, 
                                       {BigInteger.ONE, BigInteger.ZERO}};
        biA = ((n[0][0].multiply(rgbiFibonacci[0][0])).add(n[0][1].multiply(rgbiFibonacci[1][0])));
        biB = ((n[0][0].multiply(rgbiFibonacci[0][1])).add(n[0][1].multiply(rgbiFibonacci[1][1])));
        biC = ((n[1][0].multiply(rgbiFibonacci[0][0])).add(n[1][1].multiply(rgbiFibonacci[1][0])));
        biD = ((n[1][0].multiply(rgbiFibonacci[0][1])).add(n[1][1].multiply(rgbiFibonacci[1][1])));

        rgbiN[0][0] = biA;
        rgbiN[0][1] = biB;
        rgbiN[1][0] = biC;
        rgbiN[1][1] = biD;

        return (rgbiN);
    }//end multiplyMatrix(int[][], int[][])
}//end Fibonacci
import java.util.Scanner;
导入java.text.DecimalFormat;
导入java.math.biginger;
导入java.io.BufferedWriter;
导入java.io.FileWriter;
/**
*文件:
*斐波那契恒等式A^n=n+1n
*n-1
*其中n=1 A^1=1
*                          1   0
*和/或其中n对应于第n个斐波那契项
*其中,如果用户输入n,则遗嘱n-1幂中的n+1项
*这是要求的期限
*/
公共类斐波那契
{
//main(字符串[])-程序的大部分
公共静态void main(字符串[]args)
{
长l;
长链;
长鳍;
长输入=0L;
长LTEM=0L;
BigInteger biN=BigInteger.0;
BigInteger[]rgbiN=新的BigInteger[2][2];
BigInteger[]rgbibibonacci={{BigInteger.ONE,BigInteger.ONE},
{biginger.ONE,biginger.ZERO};
扫描仪键盘=新扫描仪(System.in);
DecimalFormat df=新的DecimalFormat(“#,##0”);
文件编写器;
缓冲写入器bw;
尝试
{
System.out.print(“\n输入您想要的斐波那契序列的哪个项:”;
lInput=keyboard.nextLong();
lTerm=lInput-2;
}
捕获(例外e)
{
系统出口(0);
}
lStart=System.currentTimeMillis();
rgbiN=rgbibonacci;
如果(LTEM!=-1)
{
对于(l=0;l
并通过矩阵求幂计算序列

我遇到的问题是,如果我在第n个学期运行第一个程序,我的运行时间比在第n个学期运行第二个程序要短;这与我读到的关于矩阵求幂更快的所有内容背道而驰。它的运行时也比我发现/编译/编辑的执行矩阵求幂的程序慢,以测试自己的运行时。我是什么