title: Are loops really faster than recursion?
body: <p>According to my professor loops are faster and more deficient than using recursion yet I came up with this c++ code that calculates the Fibonacci series using both recursion and loops and the results prove that they are very similar. So I maxed the possible input to see if there was a difference in performance and for some reason recursion clocked in better than using a loop. Anyone know why? Thanks in advanced.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>#include "stdafx.h"
  #include "iostream"
  #include &lt;time.h&gt;
  using namespace std;
  
  double F[200000000];
  //double F[5];
  
  /*int Fib(int num)
  {
      if (num == 0)
      {
          return 0;
      }
  
      if (num == 1)
      {
          return 1;
      }
  
      return Fib(num - 1) + Fib(num - 2);
  
  }*/
  
  double FiboNR(int n) // array of size n
  {
  
  
      for (int i = 2; i &lt;= n; i++)
      {
          F[i] = F[i - 1] + F[i - 2];
      }
      return (F[n]);
  }
  
  double FibMod(int i,int n) // array of size n
  {
      if (i==n)
      {
          return F[i];
      }
  
      F[i] = F[i - 1] + F[i - 2];
      return (F[n]);
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      /*cout &lt;&lt; "----------------Recursion--------------"&lt;&lt;endl;
      for (int i = 0; i &lt; 36; i=i+5)
      {
          clock_t tStart = clock();
          cout &lt;&lt; Fib(i);
          printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
          cout &lt;&lt; " : Fib(" &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl;
      }*/
  
      cout &lt;&lt; "----------------Linear--------------"&lt;&lt;endl;
      for (int i = 0; i &lt; 200000000; i = i + 20000000)
      //for (int i = 0; i &lt; 50; i = i + 5)
      {
          clock_t tStart = clock();
          F[0] = 0; F[1] = 1;
          cout &lt;&lt; FiboNR(i);        
          printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
          cout &lt;&lt; " : Fib(" &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl;
      }
  
      cout &lt;&lt; "----------------Recursion Modified--------------" &lt;&lt; endl;
      for (int i = 0; i &lt; 200000000; i = i + 20000000)
      //for (int i = 0; i &lt; 50; i = i + 5)
      {
          clock_t tStart = clock();
          F[0] = 0; F[1] = 1;
          cout &lt;&lt; FibMod(0,i);
          printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
          cout &lt;&lt; " : Fib(" &lt;&lt; i &lt;&lt; ")" &lt;&lt; endl;
      }
  
      std::cin.ignore();
      return 0;
  }
  </code></pre>