title: Unusual Speed Difference between Python and C++
body: <p>I recently wrote a short algorithm to calculate <a href="http://en.wikipedia.org/wiki/Happy_Numbers" rel="noreferrer">happy numbers</a> in python. The program allows you to pick an upper bound and it will determine all the happy numbers below it. For a speed comparison I decided to make the most direct translation of the algorithm I knew of from python to c++. </p>
  
  <p>Surprisingly, the c++ version runs significantly slower than the python version. Accurate speed tests between the execution times for discovering the first 10,000 happy numbers indicate the python program runs on average in 0.59 seconds and the c++ version runs on average in 8.5 seconds. </p>
  
  <p>I would attribute this speed difference to the fact that I had to write helper functions for parts of the calculations (for example determining if an element is in a list/array/vector) in the c++ version which were already built in to the python language. </p>
  
  <p>Firstly, is this the true reason for such an absurd speed difference, and secondly, how can I change the c++ version to execute more quickly than the python version (the way it should be in my opinion). </p>
  
  <p>The two pieces of code, with speed testing are here: <a href="http://dawnofdigital.net/script/happy.py.txt" rel="noreferrer">Python Version</a>, <a href="http://dawnofdigital.net/script/happy.cpp.txt" rel="noreferrer">C++ Version</a>. Thanks for the help.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;ctime&gt;
  #include &lt;windows.h&gt;
  
  using namespace std;
  
  bool inVector(int inQuestion, vector&lt;int&gt; known);
  int sum(vector&lt;int&gt; given);
  int pow(int given, int power);
  void calcMain(int upperBound);
  
  int main()
  {
      while(true)
      {
          int upperBound;
          cout &lt;&lt; "Pick an upper bound: ";
          cin &gt;&gt; upperBound;
          long start, end;
          start = GetTickCount();
          calcMain(upperBound);
          end = GetTickCount();
          double seconds = (double)(end-start) / 1000.0;
          cout &lt;&lt; seconds &lt;&lt; " seconds." &lt;&lt; endl &lt;&lt; endl;
      }
      return 0;
  }
  
  void calcMain(int upperBound)
  {
      vector&lt;int&gt; known;
      for(int i = 0; i &lt;= upperBound; i++)
      {
          bool next = false;
          int current = i;
          vector&lt;int&gt; history;
          while(!next)
          {
              char* buffer = new char[10];
              itoa(current, buffer, 10);
              string digits = buffer;
              delete buffer;
              vector&lt;int&gt; squares;
              for(int j = 0; j &lt; digits.size(); j++)
              {
                  char charDigit = digits[j];
                  int digit = atoi(&amp;charDigit);
                  int square = pow(digit, 2);
                  squares.push_back(square);
              }
              int squaresum = sum(squares);
              current = squaresum;
              if(inVector(current, history))
              {
                  next = true;
                  if(current == 1)
                  {
                      known.push_back(i);
                      //cout &lt;&lt; i &lt;&lt; "\t";
                  }
              }
              history.push_back(current);
          }
      }
      //cout &lt;&lt; "\n\n";
  }
  
  bool inVector(int inQuestion, vector&lt;int&gt; known)
  {
      for(vector&lt;int&gt;::iterator it = known.begin(); it != known.end(); it++)
          if(*it == inQuestion)
              return true;
      return false;
  }
  
  int sum(vector&lt;int&gt; given)
  {
      int sum = 0;
      for(vector&lt;int&gt;::iterator it = given.begin(); it != given.end(); it++)
          sum += *it;
      return sum;
  }
  
  int pow(int given, int power)
  {
      int original = given;
      int current = given;
      for(int i = 0; i &lt; power-1; i++)
          current *= original;
      return current;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>#!/usr/bin/env python
  
  import timeit
  
  upperBound = 0
  
  def calcMain():
      known = []
      for i in range(0,upperBound+1):
          next = False
          current = i
          history = []
          while not next:
              digits = str(current)
              squares = [pow(int(digit), 2) for digit in digits]
              squaresum = sum(squares)
              current = squaresum
              if current in history:
                  next = True
                  if current == 1:
                      known.append(i)
                      ##print i, "\t",
              history.append(current)
      ##print "\nend"
  
  while True:    
      upperBound = input("Pick an upper bound: ")
      result = timeit.Timer(calcMain).timeit(1)
      print result, "seconds.\n"
  </code></pre>