title: Huge speed difference between identical c and c++ programs
body: <p>I'm very new to c and c++ programming, so I'm starting with the basics.  I wrote identical Fibonacci loop programs for both c and c++ to test relative speed.  I thought they'd be about the same for something so simple, but the c++ version is 60x slower.  All they do is loop through and print the first 14 Fibonacci numbers 10,000 times.  Here is the c version: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main (){
      int c = 0;
      int x, y, z;
  
      while(c &lt; 10000)
      {
          x = 0;
          y = 1;
          while(x &lt; 255)
          {
              printf("%d\n", x);
              z = x + y;
              x = y;
              y = z;
          }
          c++;
      }
      return 0;
  }
  </code></pre>
  
  <p>and here is the c++ version:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int c = 0, x = 0, y = 0, z = 0;
      while(c &lt; 10000)
      {
          x = 0;
          y = 1;
          while(x &lt; 255)
          {
              cout &lt;&lt; x &lt;&lt; endl;
              z = x + y;
              x = y;
              y = z;
          }
          c++;
      }
      return 0;
  }
  </code></pre>
  
  <p>I wrote both in notepad++ and compiled them using g++ from the mingw that comes with codeblocks:</p>
  
  <pre><code>g++ -o fibc.exe fib.c -s
  g++ -o fibcpp.exe fib.cpp -s
  </code></pre>
  
  <p>The executables are very different in size: the c is 8.5KB and the c++ is 784KB! I used powershell to time them:</p>
  
  <pre><code>Measure-Command {start-process "C:\Path\fibcpp.exe" -RedirectStandardOutput "C:\Path\cpp.txt" -Wait}
  </code></pre>
  
  <p>The files produced are identical, but the c version took 1 sec and the c++ verison took 60sec! (In fact, putting a loop of 1 million for the c program still only took 13sec). I also wrote the c++ in Visual Studio 17 and compiled it there with an x86 release config.  The program size is now 9.5KB, but the run time is the same as the g++ version: 62sec.  Why is this happening for such a simple program?</p>