Hi Victor,
On 26 September 2016 at 16:09, Victor Blomqvist <
v...@viblo.se> wrote:
> All of those things helps quite a bit. My first code ran 4.3x slower than
> the ctypes version, by ditching the list its 2.1x slower, and by using
> __new__ 1.9x slower. Still a lot slower than ctypes, but at least a big
> improvement! If I dont use my python Vec2d at all and instead just pass
> around the cdata its still 1.5x slower, so 1.9x should be a reasonable
> number I guess. Are there any specific reasons why cffi is slower? Seems
> like quite a big difference compared to ctypes?
The next thing to tweak would be not have C functions that return
structs. This involves slower paths through the code, both in CPython
and in PyPy. I have no real clue if it's going to be faster or slower
on CPython, because it involves a bit more pure Python code; you need
to try. Fwiw it should definitely be faster on PyPy. For example:
typedef struct cpVect{cpFloat x,y;} cpVect;
typedef struct cpBody cpBody;
void cpBodyGetPosition(const cpBody *body, cpVect *result);
class Body(object):
...
def get_position(self):
vec = ffi.new("cpVect *")
lib.cpBodyGetPosition(self._body, vec)
return Vec2d(vec.x, vec.y)
Note that in general, getting the best possible CPython performance is
not high on the list of priorities. I prefer not to introduce 27
hints like "don't release the GIL when calling this function here".
A bientôt,
Armin.