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.
I would go with this:
I left out the
abs
since it's not necessary. But it could be that the compiler is able to optimise unsigned multiplication better.Using is like this:
I hope this helps.
concerning your code: it's probably OK, though I would make the
sum
andi
unsigned. You can addconst
to the array parameter type, but the compiler most certainly is able to figure that out on its own. Oh, and I think you should remove thatfloor
. Integer division does that already.