如何返回方法完成其工作所需的时间?

发布于 2024-09-24 08:48:53 字数 356 浏览 5 评论 0原文

我有一个简单的递归算法,它返回斐波那契数:

private static double fib_recursive(int n){
    if(n <= 2) return 1;
    else return fib_recursive(n-1) + fib_recursive(n-2);
}

现在我的任务是返回时间,该方法用它来计算a上的第400个斐波那契数给定计算机,例如 fib_recursive(400)。 “Would”以粗体显示,因为我无法运行该函数,因为该方法需要很长时间才能给出答案。

如何才能最好地实现这一目标?

I have a simple recursive algorithm, which returns Fibonacci numbers:

private static double fib_recursive(int n){
    if(n <= 2) return 1;
    else return fib_recursive(n-1) + fib_recursive(n-2);
}

Now my task is to return the time, which this method would take to calculate the 400-th fibonacci number on a given computer e.g. fib_recursive(400). "Would" is in bold, because I can't run the function, as it would take to long for this method to give an answer.

How can it be best achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水波映月 2024-10-01 08:48:53

计算每次递归调用需要多少时间,算出有多少次递归调用,你就有了答案。

有更快的方法可以使用更智能的递归算法来实现此目的,但对于您的算法,只需输入一些计时信息即可。

Calculate how much time to do each recursive call, figure out how many recursive calls, and you have an answer.

There are faster ways to do this, using a smarter recursion algorithm, but for your algorithm just put in some timing information.

要走就滚别墨迹 2024-10-01 08:48:53

通过在您想要测量的值之前和之后获取 System.currenTimeMillis()System.nanoTime() 的差异来完成计时。

Timing is done by taking differences of System.currenTimeMillis() or System.nanoTime() before and after what you want to meausre.

源来凯始玺欢你 2024-10-01 08:48:53

我最终得到的可能不是最好的解决方案,但这是我得到的(也许它会对将来的某人有所帮助):

1)我测量了递归方法计算 42 次所需的时间(例如) 斐波那契数列。

2)使用迭代方法,我计算了用递归方法计算第42个斐波那契数时执行了多少程序行。 (Rows = 3*fib_iterative(42)-2)

3) 将 2. 除以 1。我得到了 1 毫秒内执行的平均行数。

4)使用迭代方法,我计算了用递归方法计算第400个斐波那契数时执行了多少程序行。 (行 = 3*fib_iterative(400)-2)

5) 将 4 除以 3。我得到了 fib_recursive(400) 花费的估计时间。

// Recursive algorithm, calculates Fibonacci number (slow)
private static double fib_recursive(int n){
    if( n <= 2) return 1;
    else return fib_recursive(n-1) + fib_recursive(n-2);
}

// Iterative algorithm, calculates Fibonacci number (fast)
private static double fib_iterative(int n){
    double a = 1, b = 1;
    for (int i = 2; i <= n; i++){
        b = a + b;
        a = b - a;
    }
    return a;
}

// Get time, which fib_recursive(int) needs to calculate the 400th Fibonacci number
private static long testRecursiveFor400(){
    int elapsedTime = 0;
    int start = (int) System.currentTimeMillis();
    fib_recursive(42);
    elapsedTime = (int) (System.currentTimeMillis() - start);
    return (3 * (long) fib_iterative(400) - 2) / ((3 * (int) fib_iterative(42) - 2) / elapsedTime);
}

I ended up with probably not a best solution, but here is what i've got(may be it will help someone in future):

1) I measured the time, which the recursive method needs to calculate the 42-nd(for example) Fibonacci number.

2) Using the iterative method, I counted how many program rows are executed while calculating the 42-nd Fibonacci number with the recursive method. (Rows = 3*fib_iterative(42)-2)

3) Deviding 2. by 1. I got the average number of rows executed in 1 millisecond.

4) Using the iterative method, I counted how many program rows are executed while calculating the 400-th Fibonacci number with the recursive method. (Rows = 3*fib_iterative(400)-2)

5) Deviding 4. by 3. I got the estimated time spent by fib_recursive(400).

// Recursive algorithm, calculates Fibonacci number (slow)
private static double fib_recursive(int n){
    if( n <= 2) return 1;
    else return fib_recursive(n-1) + fib_recursive(n-2);
}

// Iterative algorithm, calculates Fibonacci number (fast)
private static double fib_iterative(int n){
    double a = 1, b = 1;
    for (int i = 2; i <= n; i++){
        b = a + b;
        a = b - a;
    }
    return a;
}

// Get time, which fib_recursive(int) needs to calculate the 400th Fibonacci number
private static long testRecursiveFor400(){
    int elapsedTime = 0;
    int start = (int) System.currentTimeMillis();
    fib_recursive(42);
    elapsedTime = (int) (System.currentTimeMillis() - start);
    return (3 * (long) fib_iterative(400) - 2) / ((3 * (int) fib_iterative(42) - 2) / elapsedTime);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文