I am attempting to speed up a Python script by using ctypes to outsource some of the heavily lifting to C++.
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.
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++.
def shortTermEnergy(frame): return sum( [ abs(x)**2 for x in frame ] ) / len(frame) I will be passing frame as an array by using arr = (ctypes.c_int * len(frame))(*frame) converting it from a list to a nice array for C++
I hope this is the best practice and I am not missing anything glaringly obvious? It's been a long time since I wrote any C++.
Thanks
EDIT
I have gone with this C++ code for the moment, please do let me know if there are ways to improve.
#include <cmath> extern "C" int square(int size, int array[]) { int sum = 0; for (int i = 0; i < size; i++) { int number = array[i]; int value = (number * number); sum = sum + value; } return floor(sum / size); } Where size is the len() of the array passed from Python.
frameis represented in your c++ code (native array / stl vector) - not sure that the efficiency will vary that much from one option to another, but there are different (shorter / longer) ways of implementing it depending onframe's type.