Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:30:17+00:00 2026-06-17T06:30:17+00:00

If I run the following code: public static void main(String[] argsv) { long whichFib

  • 0

If I run the following code:

public static void main(String[] argsv) {

    long whichFib = 45;
    long st;
    st = System.currentTimeMillis();
    System.out.println(recursiveFib(whichFib));
    System.out.println("Recursive version took " + (System.currentTimeMillis() - st) + " milliseconds.");

    st = System.currentTimeMillis();
    System.out.println(iterativeFib(whichFib));
    System.out.println("Iterative version took " + (System.currentTimeMillis() - st) + " milliseconds.");

}

public static long recursiveFib(long n) {

    if (n == 0)
        return 0;
    if (n == 1 || n == 2)
        return 1;

    return recFib(n - 1) + recFib(n - 2);
}

public static long iterativeFib(long n) {

    if (n == 0)
        return 0;
    else if (n == 1 || n == 2)
        return 1;

    long sum = 1;
    long p = 1;
    long u = 1;

    for (int i = 2; i < n; i++) {
        sum = p + u;
        p = u;
        u = sum;
    }

    return sum;
}

I get the following output:

1134903170
Recursive version took 5803 milliseconds.
1134903170
Iterative version took 0 milliseconds.

I feel like I have done something incorrect here. I thought that the tail call (the final line in the recursive fibonacci method) would be optimised by the compiler, bringing it closer in speed to the iterative version. Does anyone have any ideas why this is running so slowly? Is it just a poorly written function?

N.B. I am using Oracle JDK 1.7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:30:19+00:00Added an answer on June 17, 2026 at 6:30 am

    As other answers noted, your function is not tail recursive, here is a tail recursive version of fibonacci:

    long fibonacci(int n) {
        if (n == 0)
            return 0;
        else
            return fibonacciTail(n, 1, 0, 1);
    }
    
    long fibonacciTail(int n, int m, long fibPrev, long fibCurrent) {
        if (n == m)
            return fibCurrent;
        else
            return fibonacciTail(n, m + 1, fibCurrent, fibPrev + fibCurrent);
    }
    

    Also, the JVM does not do tail-call optimization so a stack frame will be allocated for every recursive call, making this quite an expensive operation. However it is important to note this is technically implementation dependent, see comments for link to IBM SDK that does TCO, and this SO question for more information.

    An optimized version would be to do tail-call optimization manually, converting the above to a while loop with variable reassignment:

    long fibonacciIter(int n) {
        int m = 1;
        long fibPrev = 0;
        long fibCurrent = 1;
        while (n != m) {
            m = m + 1;
            int current = fibCurrent;
            fibCurrent = fibPrev + fibCurrent;
            fibPrev = current;
        }
        return fibCurrent;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run a class with the following code: public static void main(String[] args)
When I run the following code: public class LianXi1 { public static void main(String
I am trying to run the following piece of code: public static void main(String[]
I've got the following code: public class testMatch { public static void main(String[] args)
Consider the following code: class Sample{ public static void main(String args[]){ String text1=C:\Documents\User\sample; String
I have the following code: public static void main(String[] args) { try { int
If I have the following dummy code: public static void main(String[] args) { TestRunnable
I'm running the following code: public class SkipTest { public static void main(String[] args)
When I run the following code: public class Test { Test(){ System.out.println(1); } {
If i have the following code : public static void main(String [] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.