title: Speeding up Python with C++
body: <p>I am attempting to speed up a Python script by using ctypes to outsource some of the heavily lifting to C++.</p>
  
  <p>I have this up and running quite nicely with a small example (returning x^2) but now it is time to set up my function in this configuration.</p>
  
  <p>My question is, how would one write this function of Python code nicely in C++ to ensure its as quick as possible, I would hate to think I might not get any speed increase, simply because of my sub-par C++.</p>
  
  <pre><code>def shortTermEnergy(frame):
      return sum( [ abs(x)**2 for x in frame ] ) / len(frame)
  </code></pre>
  
  <p>I will be passing <code>frame</code> as an array by using <code>arr = (ctypes.c_int * len(frame))(*frame)</code> converting it from a list to a nice array for C++</p>
  
  <p>I hope this is the best practice and I am not missing anything glaringly obvious? It's been a <em>long</em> time since I wrote any C++.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have gone with this C++ code for the moment, please do let me know if there are ways to improve.</p>
  
  <pre><code>#include &lt;cmath&gt;
  
  extern "C" int square(int size, int array[])
  {
      int sum = 0;
  
      for (int i = 0; i &lt; size; i++)
      {
          int number = array[i];
          int value = (number * number);
          sum = sum + value;
      }
  
      return floor(sum / size);
  }
  </code></pre>
  
  <p>Where <code>size</code> is the len() of the array passed from Python.</p>