Java斐波纳契数字计时器

时间:2018-02-04 04:36:20

标签: java debugging timer

程序本身运行良好。我输入的每个值都返回正确的相应Fibonacci数。我还需要使用循环来计算递归函数和函数的执行时间。当我运行程序时,它只返回用户实际输入输入所花费的时间,而不是运行函数所花费的时间。我试过移动计时器,但我一直得到相同的结果。我想返回实际用来递归计算斐波那契数的时间以及使用循环计算斐波那契数所需的时间。

import java.util.Scanner;

public class FibNumbers {
            public static void main(String[] args) {
            int f;
            int l;
            long num1;
            long num2;
            int choice;
            long t1 = System.currentTimeMillis();
            Scanner sc = new Scanner(System.in);
            Scanner keyboard = new Scanner(System.in);

            System.out.println("For recursion press 1 %n for loop press 2" );
            choice = keyboard.nextInt();
            if(choice == 1) {
                System.out.println("Please enter a value for n");
                f = Integer.parseInt(sc.nextLine());
                System.out.println("The Fibonacci number is " + fibonacciR(f) + " using recursion");
            }
            else {
                System.out.println("Please enter a value for n");
                l = Integer.parseInt(sc.nextLine());
                System.out.println("The Fibonacci number is " + fibonacciL(l) + " using a for loop");
            }
            long t2 = System.currentTimeMillis();
            System.out.println("The elapsed time is " + (t2 - t1) / 1000 + " seconds.");
        }

        public static long fibonacciR(long f) {
            if(f == 0) {
                return 0;
            }
            else if(f == 1) {
                return 1;
            }
            else {
                return fibonacciR(f-1) + fibonacciR(f-2);
            }
        }

        public static long fibonacciL(long l) {
            long num1 = 0;
            long num2 = 1;
            long total = l;
            long sumOfTwo;
            for(int i = 0; i < total; i++) {
                sumOfTwo = num1 + num2;
                num1 = num2;
                num2 = sumOfTwo;
            }
            return num1;
        }
    }

3 个答案:

答案 0 :(得分:1)

您应该在用户输入t1 = System.currentTimeMillis();的值后立即初始化您的n

long t1;
if(choice == 1) {
    System.out.println("Please enter a value for n");
    f = Integer.parseInt(sc.nextLine());
    t1 = System.currentTimeMillis();
    System.out.println("The Fibonacci number is " + fibonacciR(f) + " using recursion");
}
else {
    System.out.println("Please enter a value for n");
    l = Integer.parseInt(sc.nextLine());
    t1 = System.currentTimeMillis();
    System.out.println("The Fibonacci number is " + fibonacciL(l) + " using a for loop");
}
long t2 = System.currentTimeMillis();

答案 1 :(得分:1)

使用System.nanoTime();纳秒。

如果(t2 - t1)小于1000,则除以1000将得到0,因为你使用long你可以转换为float / double然后进行除法。

这样的事情可能是:

import java.util.Scanner;

public class FibNumbers {
        public static void main(String[] args) {
        int f;
        int l;
        long num1;
        long num2;
        int choice;
        long t1 = 0, t2 = 0;
        Scanner sc = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);

        System.out.println("For recursion press 1 %n for loop press 2" );
        choice = keyboard.nextInt();
        if(choice == 1) {
            System.out.println("Please enter a value for n");
            f = Integer.parseInt(sc.nextLine());
            t1 = System.nanoTime();
            System.out.println("The Fibonacci number is " + fibonacciR(f) + " using recursion");
            t2 = System.nanoTime();
        }
        else {
            System.out.println("Please enter a value for n");
            l = Integer.parseInt(sc.nextLine());
            t1 = System.currentTimeMillis();
            System.out.println("The Fibonacci number is " + fibonacciL(l) + " using a for loop");
            t2 = System.nanoTime();
        }
        System.out.println("The elapsed time is " + (t2 - t1) + " nano seconds.");
    }

    public static long fibonacciR(long f) {
        if(f == 0) {
            return 0;
        }
        else if(f == 1) {
            return 1;
        }
        else {
            return fibonacciR(f-1) + fibonacciR(f-2);
        }
    }

    public static long fibonacciL(long l) {
        long num1 = 0;
        long num2 = 1;
        long total = l;
        long sumOfTwo;
        for(int i = 0; i < total; i++) {
            sumOfTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfTwo;
        }
        return num1;
    }
}

答案 2 :(得分:1)

class FibonacciRecursionElseStrategy implements Fibonacci {

    @Override
    public long calculate(long f) {

        if(f < 0)
            throw new IllegalArgumentException();

        if(f == 0) {
            return 0;
        } else if(f == 1) {
            return 1;
        } else {
            return calculate(f-1) + calculate(f-2);
        }
    }

    @Override
    public void elapsedTime(long f, int repetitions) {

        long t1, t2;

        t1 = System.nanoTime();

        for (int i = 0; i < repetitions; i++) 
            calculate(f);

        t2 = System.nanoTime();

        System.out.println("The elapsed time is " + ((t2 - t1) / 1000000) + " [ms] using a for recursionElseIf strategy");

    }

}

class FibonacciRecursionStrategy implements Fibonacci {

    @Override
    public long calculate(long f) {

        if(f < 0)
            throw new IllegalArgumentException();

        return f == 0 || f == 1 ? f : calculate(f - 1) + calculate(f - 2);
    }

    @Override
    public void elapsedTime(long f, int repetitions) {

        long t1, t2;

        t1 = System.nanoTime();

        for (int i = 0; i < repetitions; i++) 
            calculate(f);

        t2 = System.nanoTime();

        System.out.println("The elapsed time is " + ((t2 - t1) / 1000000) + " [ms] using a for recursion strategy");

    }

}

class FibonacciLoopStrategy implements Fibonacci {

    @Override
    public long calculate(long f) {

        if(f < 0)
            throw new IllegalArgumentException();

        long num1 = 0;
        long num2 = 1;
        long sumOfTwo;

        for(int i = 0; i < f; i++) {

            sumOfTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfTwo;
        }

        return num1;
    }

    @Override
    public void elapsedTime(long f, int repetitions) {

        long t1, t2;

        t1 = System.nanoTime();

        for (int i = 0; i < repetitions; i++) 
            calculate(f);

        t2 = System.nanoTime();

        System.out.println("The elapsed time is " + ((t2 - t1) / 1000000) + " [ms] using a for loop strategy");

    }

}

interface Fibonacci {

    public long calculate(long f);
    public void elapsedTime(long f, int repetitions);

}

public class FibonacciNumbers {

    public static long fibonacciRecursion(long f) {
        return f == 0 || f == 1 ? f : fibonacciRecursion(f - 1) + fibonacciRecursion(f - 2);
    }

    public static long fibonacciRecursionElseIf(long f) {

        if(f == 0) {
            return 0;
        } else if(f == 1) {
            return 1;
        } else {
            return fibonacciRecursionElseIf(f-1) + fibonacciRecursionElseIf(f-2);
        }
    }

    public static long fibonacciLoop(long total) {

        long num1 = 0;
        long num2 = 1;
        long sumOfTwo;

        for(int i = 0; i < total; i++) {

            sumOfTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfTwo;
        }

        return num1;
    }
}

class Timer {

    public static void elapsedTimeFibonacciLoop(long value, int repetitions) {

        long t1, t2;

        t1 = System.nanoTime();

        for (int i = 0; i < repetitions; i++) 
            FibonacciNumbers.fibonacciLoop(value);

        t2 = System.nanoTime();

        System.out.println("The elapsed time is " + ((t2 - t1) / 1000000) + " [ms] using a for loop");
    }

    public static void elapsedTimeFibonacciRecursion(long value, int repetitions) {

        long t1, t2;

        t1 = System.nanoTime();

        for (int i = 0; i < repetitions; i++) 
            FibonacciNumbers.fibonacciRecursion(value);

        t2 = System.nanoTime();

        System.out.println("The elapsed time is " + ((t2 - t1) / 1000000) + " [ms] using a for recursion");
    }

    public static void elapsedTimeFibonacciRecursionElseIf(long value, int repetitions) {

        long t1, t2;

        t1 = System.nanoTime();

        for (int i = 0; i < repetitions; i++) 
            FibonacciNumbers.fibonacciRecursionElseIf(value);

        t2 = System.nanoTime();

        System.out.println("The elapsed time is " + ((t2 - t1) / 1000000) + " [ms] using a for recursionElseIf");
    }
}

class Start {

    public static void main(String[] args) {

        Timer.elapsedTimeFibonacciLoop(11, 10000000);
        Timer.elapsedTimeFibonacciRecursion(11, 10000000);
        Timer.elapsedTimeFibonacciRecursionElseIf(11, 10000000);

        System.out.println("" + FibonacciNumbers.fibonacciLoop(11) + " for loop");
        System.out.println("" + FibonacciNumbers.fibonacciRecursion(11) + " for recursion");
        System.out.println("" + FibonacciNumbers.fibonacciRecursionElseIf(11) + " for recursionElseIf");

        System.out.println("Strategy");
        Fibonacci loop = new FibonacciLoopStrategy();
        Fibonacci recursion = new FibonacciRecursionStrategy();
        Fibonacci recursionElse = new FibonacciRecursionElseStrategy();

        loop.elapsedTime(11, 10000000);
        recursion.elapsedTime(11, 10000000);
        recursionElse.elapsedTime(11, 10000000);

    }
}

输出:

使用for循环
经过的时间是157 [ms] 使用递归的经过时间是7591 [ms] 经过的时间是7213 [ms],使用for recursionElseIf
89 for loop
89用于递归
89 recursionElseIf
战略
使用for循环策略的经过时间为158 [ms] 使用for递归策略的经过时间是8094 [ms] 使用for recursionElseIf策略的经过时间是8183 [ms]

通过改变循环和重复计算方法的顺序:

class Start {

    public static void main(String[] args) {

        Timer.elapsedTimeFibonacciRecursion(11, 10000000);
        Timer.elapsedTimeFibonacciLoop(11, 10000000);
        Timer.elapsedTimeFibonacciRecursionElseIf(11, 10000000);

        System.out.println("" + FibonacciNumbers.fibonacciLoop(11) + " for loop");
        System.out.println("" + FibonacciNumbers.fibonacciRecursion(11) + " for recursion");
        System.out.println("" + FibonacciNumbers.fibonacciRecursionElseIf(11) + " for recursionElseIf");

        System.out.println("Strategy");
        Fibonacci loop = new FibonacciLoopStrategy();
        Fibonacci recursion = new FibonacciRecursionStrategy();
        Fibonacci recursionElse = new FibonacciRecursionElseStrategy();

        recursion.elapsedTime(11, 10000000);
        loop.elapsedTime(11, 10000000);
        recursionElse.elapsedTime(11, 10000000);

    }
}

使用for递归的经过时间是7690 [ms] 使用for循环
经过的时间是172 [ms] 经过的时间是7728 [ms],使用for recursionElseIf
89 for loop
89用于递归
89 recursionElseIf
战略
使用for递归策略的经过时间是8407 [ms] 使用for循环策略的经过时间为155 [ms] 使用for recursionElseIf策略的经过时间是7989 [ms]